/src/openssl/ssl/statem/extensions.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2026 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 | | #if defined(__TANDEM) && defined(_SPT_MODEL_) |
11 | | #include <spthread.h> |
12 | | #include <spt_extensions.h> /* timeval */ |
13 | | #endif |
14 | | |
15 | | #include <string.h> |
16 | | #include "internal/nelem.h" |
17 | | #include "internal/cryptlib.h" |
18 | | #include "internal/ssl_unwrap.h" |
19 | | #include "../ssl_local.h" |
20 | | #include "statem_local.h" |
21 | | #include <openssl/ocsp.h> |
22 | | #include <openssl/core_names.h> |
23 | | |
24 | | /* |
25 | | * values for ext_defs ech_handling field |
26 | | * exceptionally, we don't conditionally compile that field to avoid a pile of |
27 | | * ifndefs all over the ext_defs values |
28 | | */ |
29 | 0 | #define OSSL_ECH_HANDLING_CALL_BOTH 1 /* call constructor both times */ |
30 | 0 | #define OSSL_ECH_HANDLING_COMPRESS 2 /* compress outer value into inner */ |
31 | | #define OSSL_ECH_HANDLING_DUPLICATE 3 /* same value in inner and outer */ |
32 | | /* |
33 | | * DUPLICATE isn't really useful other than to show we can, |
34 | | * and for debugging/tests/coverage so may disappear. Changes mostly |
35 | | * won't affect the outer CH size, due to padding, but might for some |
36 | | * larger extensions. |
37 | | * |
38 | | * Note there is a co-dependency with test/recipes/75-test_quicapi.t: |
39 | | * If you change an |ech_handling| value, that may well affect the order |
40 | | * of extensions in a ClientHello, which is reflected in the test data |
41 | | * in test/recipes/75-test_quicapi_data/\*.txt files. To fix, you need |
42 | | * to look in test-runs/test_quicapi for the "new" files and then edit |
43 | | * (replacing actual octets with "?" in relevant places), and copy the |
44 | | * result back over to test/recipes/75-test_quicapi_data/. The reason |
45 | | * this happens is the ECH COMPRESS'd extensions need to be contiguous |
46 | | * in the ClientHello, so changes to/from COMPRESS affect extension |
47 | | * order, in inner and outer CH. There doesn't seem to be an easy, |
48 | | * generic, way to reconcile these compile-time changes with having |
49 | | * fixed value test files. Likely the best option is to decide on the |
50 | | * disposition of ECH COMPRESS or not and consider that an at least |
51 | | * medium-term thing. (But still allow other builds to vary at |
52 | | * compile time if they need something different.) |
53 | | */ |
54 | | #ifndef OPENSSL_NO_ECH |
55 | | static int init_ech(SSL_CONNECTION *s, unsigned int context); |
56 | | static int final_ech(SSL_CONNECTION *s, unsigned int context, int sent); |
57 | | #endif /* OPENSSL_NO_ECH */ |
58 | | |
59 | | static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent); |
60 | | static int init_server_name(SSL_CONNECTION *s, unsigned int context); |
61 | | static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent); |
62 | | static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context, |
63 | | int sent); |
64 | | static int init_session_ticket(SSL_CONNECTION *s, unsigned int context); |
65 | | #ifndef OPENSSL_NO_OCSP |
66 | | static int init_status_request(SSL_CONNECTION *s, unsigned int context); |
67 | | #endif |
68 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
69 | | static int init_npn(SSL_CONNECTION *s, unsigned int context); |
70 | | #endif |
71 | | static int init_alpn(SSL_CONNECTION *s, unsigned int context); |
72 | | static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent); |
73 | | static int init_sig_algs_cert(SSL_CONNECTION *s, unsigned int context); |
74 | | static int init_sig_algs(SSL_CONNECTION *s, unsigned int context); |
75 | | static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context); |
76 | | static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context); |
77 | | static int init_certificate_authorities(SSL_CONNECTION *s, |
78 | | unsigned int context); |
79 | | static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s, |
80 | | WPACKET *pkt, |
81 | | unsigned int context, |
82 | | X509 *x, |
83 | | size_t chainidx); |
84 | | static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt, |
85 | | unsigned int context, X509 *x, |
86 | | size_t chainidx); |
87 | | #ifndef OPENSSL_NO_SRP |
88 | | static int init_srp(SSL_CONNECTION *s, unsigned int context); |
89 | | #endif |
90 | | static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context); |
91 | | static int init_etm(SSL_CONNECTION *s, unsigned int context); |
92 | | static int init_ems(SSL_CONNECTION *s, unsigned int context); |
93 | | static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent); |
94 | | static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context); |
95 | | static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent); |
96 | | #ifndef OPENSSL_NO_SRTP |
97 | | static int init_srtp(SSL_CONNECTION *s, unsigned int context); |
98 | | #endif |
99 | | static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent); |
100 | | static int final_supported_versions(SSL_CONNECTION *s, unsigned int context, |
101 | | int sent); |
102 | | static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent); |
103 | | static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context, |
104 | | int sent); |
105 | | static int init_post_handshake_auth(SSL_CONNECTION *s, unsigned int context); |
106 | | static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent); |
107 | | static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context); |
108 | | static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt, |
109 | | unsigned int context, |
110 | | X509 *x, size_t chainidx); |
111 | | static int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, |
112 | | unsigned int context, |
113 | | X509 *x, size_t chainidx); |
114 | | |
115 | | /* Structure to define a built-in extension */ |
116 | | typedef struct extensions_definition_st { |
117 | | /* The defined type for the extension */ |
118 | | unsigned int type; |
119 | | /* |
120 | | * The context that this extension applies to, e.g. what messages and |
121 | | * protocol versions |
122 | | */ |
123 | | unsigned int context; |
124 | | /* |
125 | | * exceptionally, we don't conditionally compile this field to avoid a |
126 | | * pile of ifndefs all over the ext_defs values |
127 | | */ |
128 | | int ech_handling; /* how to handle ECH for this extension type */ |
129 | | /* |
130 | | * Initialise extension before parsing. Always called for relevant contexts |
131 | | * even if extension not present |
132 | | */ |
133 | | int (*init)(SSL_CONNECTION *s, unsigned int context); |
134 | | /* Parse extension sent from client to server */ |
135 | | int (*parse_ctos)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
136 | | X509 *x, size_t chainidx); |
137 | | /* Parse extension send from server to client */ |
138 | | int (*parse_stoc)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
139 | | X509 *x, size_t chainidx); |
140 | | /* Construct extension sent from server to client */ |
141 | | EXT_RETURN (*construct_stoc)(SSL_CONNECTION *s, WPACKET *pkt, |
142 | | unsigned int context, |
143 | | X509 *x, size_t chainidx); |
144 | | /* Construct extension sent from client to server */ |
145 | | EXT_RETURN (*construct_ctos)(SSL_CONNECTION *s, WPACKET *pkt, |
146 | | unsigned int context, |
147 | | X509 *x, size_t chainidx); |
148 | | /* |
149 | | * Finalise extension after parsing. Always called where an extensions was |
150 | | * initialised even if the extension was not present. |sent| is set to 1 if |
151 | | * the extension was seen, or 0 otherwise. |
152 | | */ |
153 | | int (*final)(SSL_CONNECTION *s, unsigned int context, int sent); |
154 | | } EXTENSION_DEFINITION; |
155 | | |
156 | | /* |
157 | | * Definitions of all built-in extensions. NOTE: Changes in the number or order |
158 | | * of these extensions should be mirrored with equivalent changes to the |
159 | | * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h. |
160 | | * Extensions should be added to test/ext_internal_test.c as well, as that |
161 | | * tests the ordering of the extensions. |
162 | | * |
163 | | * Each extension has an initialiser, a client and |
164 | | * server side parser and a finaliser. The initialiser is called (if the |
165 | | * extension is relevant to the given context) even if we did not see the |
166 | | * extension in the message that we received. The parser functions are only |
167 | | * called if we see the extension in the message. The finalisers are always |
168 | | * called if the initialiser was called. |
169 | | * There are also server and client side constructor functions which are always |
170 | | * called during message construction if the extension is relevant for the |
171 | | * given context. |
172 | | * The initialisation, parsing, finalisation and construction functions are |
173 | | * always called in the order defined in this list. Some extensions may depend |
174 | | * on others having been processed first, so the order of this list is |
175 | | * significant. |
176 | | * The extension context is defined by a series of flags which specify which |
177 | | * messages the extension is relevant to. These flags also specify whether the |
178 | | * extension is relevant to a particular protocol or protocol version. |
179 | | * |
180 | | * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at |
181 | | * the end, keep these extensions before signature_algorithm. |
182 | | */ |
183 | | #define INVALID_EXTENSION { TLSEXT_TYPE_invalid, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL } |
184 | | |
185 | | static const EXTENSION_DEFINITION ext_defs[] = { |
186 | | { TLSEXT_TYPE_renegotiate, |
187 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
188 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
189 | | OSSL_ECH_HANDLING_COMPRESS, |
190 | | NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate, |
191 | | tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate, |
192 | | final_renegotiate }, |
193 | | { TLSEXT_TYPE_server_name, |
194 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
195 | | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
196 | | OSSL_ECH_HANDLING_CALL_BOTH, |
197 | | init_server_name, |
198 | | tls_parse_ctos_server_name, tls_parse_stoc_server_name, |
199 | | tls_construct_stoc_server_name, tls_construct_ctos_server_name, |
200 | | final_server_name }, |
201 | | { TLSEXT_TYPE_max_fragment_length, |
202 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
203 | | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
204 | | OSSL_ECH_HANDLING_COMPRESS, |
205 | | NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen, |
206 | | tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen, |
207 | | final_maxfragmentlen }, |
208 | | #ifndef OPENSSL_NO_SRP |
209 | | { TLSEXT_TYPE_srp, |
210 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
211 | | OSSL_ECH_HANDLING_COMPRESS, |
212 | | init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL }, |
213 | | #else |
214 | | INVALID_EXTENSION, |
215 | | #endif |
216 | | { TLSEXT_TYPE_ec_point_formats, |
217 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
218 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
219 | | OSSL_ECH_HANDLING_COMPRESS, |
220 | | init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats, |
221 | | tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats, |
222 | | final_ec_pt_formats }, |
223 | | { /* |
224 | | * "supported_groups" is spread across several specifications. |
225 | | * It was originally specified as "elliptic_curves" in RFC 4492, |
226 | | * and broadened to include named FFDH groups by RFC 7919. |
227 | | * Both RFCs 4492 and 7919 do not include a provision for the server |
228 | | * to indicate to the client the complete list of groups supported |
229 | | * by the server, with the server instead just indicating the |
230 | | * selected group for this connection in the ServerKeyExchange |
231 | | * message. TLS 1.3 adds a scheme for the server to indicate |
232 | | * to the client its list of supported groups in the |
233 | | * EncryptedExtensions message, but none of the relevant |
234 | | * specifications permit sending supported_groups in the ServerHello. |
235 | | * Nonetheless (possibly due to the close proximity to the |
236 | | * "ec_point_formats" extension, which is allowed in the ServerHello), |
237 | | * there are several servers that send this extension in the |
238 | | * ServerHello anyway. Up to and including the 1.1.0 release, |
239 | | * we did not check for the presence of nonpermitted extensions, |
240 | | * so to avoid a regression, we must permit this extension in the |
241 | | * TLS 1.2 ServerHello as well. |
242 | | * |
243 | | * Note that there is no tls_parse_stoc_supported_groups function, |
244 | | * so we do not perform any additional parsing, validation, or |
245 | | * processing on the server's group list -- this is just a minimal |
246 | | * change to preserve compatibility with these misbehaving servers. |
247 | | */ |
248 | | TLSEXT_TYPE_supported_groups, |
249 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS |
250 | | | SSL_EXT_TLS1_2_SERVER_HELLO, |
251 | | OSSL_ECH_HANDLING_COMPRESS, |
252 | | NULL, tls_parse_ctos_supported_groups, NULL, |
253 | | tls_construct_stoc_supported_groups, |
254 | | tls_construct_ctos_supported_groups, NULL }, |
255 | | { TLSEXT_TYPE_session_ticket, |
256 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
257 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
258 | | OSSL_ECH_HANDLING_COMPRESS, |
259 | | init_session_ticket, tls_parse_ctos_session_ticket, |
260 | | tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket, |
261 | | tls_construct_ctos_session_ticket, NULL }, |
262 | | #ifndef OPENSSL_NO_OCSP |
263 | | { TLSEXT_TYPE_status_request, |
264 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
265 | | | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
266 | | OSSL_ECH_HANDLING_COMPRESS, |
267 | | init_status_request, tls_parse_ctos_status_request, |
268 | | tls_parse_stoc_status_request, tls_construct_stoc_status_request, |
269 | | tls_construct_ctos_status_request, NULL }, |
270 | | #else |
271 | | INVALID_EXTENSION, |
272 | | #endif |
273 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
274 | | { TLSEXT_TYPE_next_proto_neg, |
275 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
276 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
277 | | OSSL_ECH_HANDLING_COMPRESS, |
278 | | init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn, |
279 | | tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL }, |
280 | | #else |
281 | | INVALID_EXTENSION, |
282 | | #endif |
283 | | { /* |
284 | | * Must appear in this list after server_name so that finalisation |
285 | | * happens after server_name callbacks |
286 | | */ |
287 | | TLSEXT_TYPE_application_layer_protocol_negotiation, |
288 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
289 | | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
290 | | OSSL_ECH_HANDLING_CALL_BOTH, |
291 | | init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn, |
292 | | tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn }, |
293 | | #ifndef OPENSSL_NO_SRTP |
294 | | { TLSEXT_TYPE_use_srtp, |
295 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
296 | | | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY, |
297 | | OSSL_ECH_HANDLING_COMPRESS, |
298 | | init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp, |
299 | | tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL }, |
300 | | #else |
301 | | INVALID_EXTENSION, |
302 | | #endif |
303 | | { TLSEXT_TYPE_encrypt_then_mac, |
304 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
305 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
306 | | /* |
307 | | * If you want to demonstrate/exercise duplicate, then |
308 | | * this does that and has no effect on sizes, but it |
309 | | * will break the quicapi test (see above). Probably |
310 | | * best done in local tests and not committed to any |
311 | | * upstream. |
312 | | * OSSL_ECH_HANDLING_DUPLICATE, |
313 | | */ |
314 | | OSSL_ECH_HANDLING_COMPRESS, |
315 | | init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm, |
316 | | tls_construct_stoc_etm, tls_construct_ctos_etm, NULL }, |
317 | | #ifndef OPENSSL_NO_CT |
318 | | { TLSEXT_TYPE_signed_certificate_timestamp, |
319 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
320 | | | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
321 | | OSSL_ECH_HANDLING_COMPRESS, |
322 | | NULL, |
323 | | /* |
324 | | * No server side support for this, but can be provided by a custom |
325 | | * extension. This is an exception to the rule that custom extensions |
326 | | * cannot override built in ones. |
327 | | */ |
328 | | NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL }, |
329 | | #else |
330 | | INVALID_EXTENSION, |
331 | | #endif |
332 | | { TLSEXT_TYPE_extended_master_secret, |
333 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
334 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
335 | | OSSL_ECH_HANDLING_COMPRESS, |
336 | | init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems, |
337 | | tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems }, |
338 | | { TLSEXT_TYPE_signature_algorithms_cert, |
339 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
340 | | OSSL_ECH_HANDLING_COMPRESS, |
341 | | init_sig_algs_cert, tls_parse_ctos_sig_algs_cert, |
342 | | tls_parse_ctos_sig_algs_cert, |
343 | | /* We do not generate signature_algorithms_cert at present. */ |
344 | | NULL, NULL, NULL }, |
345 | | { |
346 | | TLSEXT_TYPE_post_handshake_auth, |
347 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY, |
348 | | OSSL_ECH_HANDLING_COMPRESS, |
349 | | init_post_handshake_auth, |
350 | | tls_parse_ctos_post_handshake_auth, |
351 | | NULL, |
352 | | NULL, |
353 | | tls_construct_ctos_post_handshake_auth, |
354 | | NULL, |
355 | | }, |
356 | | { TLSEXT_TYPE_client_cert_type, |
357 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS |
358 | | | SSL_EXT_TLS1_2_SERVER_HELLO, |
359 | | OSSL_ECH_HANDLING_CALL_BOTH, |
360 | | init_client_cert_type, |
361 | | tls_parse_ctos_client_cert_type, tls_parse_stoc_client_cert_type, |
362 | | tls_construct_stoc_client_cert_type, tls_construct_ctos_client_cert_type, |
363 | | NULL }, |
364 | | { TLSEXT_TYPE_server_cert_type, |
365 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS |
366 | | | SSL_EXT_TLS1_2_SERVER_HELLO, |
367 | | OSSL_ECH_HANDLING_CALL_BOTH, |
368 | | init_server_cert_type, |
369 | | tls_parse_ctos_server_cert_type, tls_parse_stoc_server_cert_type, |
370 | | tls_construct_stoc_server_cert_type, tls_construct_ctos_server_cert_type, |
371 | | NULL }, |
372 | | { TLSEXT_TYPE_signature_algorithms, |
373 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
374 | | OSSL_ECH_HANDLING_COMPRESS, |
375 | | init_sig_algs, tls_parse_ctos_sig_algs, |
376 | | tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs, |
377 | | tls_construct_ctos_sig_algs, final_sig_algs }, |
378 | | { TLSEXT_TYPE_supported_versions, |
379 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO |
380 | | | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY, |
381 | | OSSL_ECH_HANDLING_COMPRESS, |
382 | | NULL, |
383 | | /* Processed inline as part of version selection */ |
384 | | NULL, tls_parse_stoc_supported_versions, |
385 | | tls_construct_stoc_supported_versions, |
386 | | tls_construct_ctos_supported_versions, final_supported_versions }, |
387 | | { TLSEXT_TYPE_psk_kex_modes, |
388 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY |
389 | | | SSL_EXT_TLS1_3_ONLY, |
390 | | OSSL_ECH_HANDLING_COMPRESS, |
391 | | init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL, |
392 | | tls_construct_ctos_psk_kex_modes, NULL }, |
393 | | { /* |
394 | | * Must be in this list after supported_groups. We need that to have |
395 | | * been parsed before we do this one. |
396 | | */ |
397 | | TLSEXT_TYPE_key_share, |
398 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO |
399 | | | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY |
400 | | | SSL_EXT_TLS1_3_ONLY, |
401 | | OSSL_ECH_HANDLING_COMPRESS, |
402 | | NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share, |
403 | | tls_construct_stoc_key_share, tls_construct_ctos_key_share, |
404 | | final_key_share }, |
405 | | { /* Must be after key_share */ |
406 | | TLSEXT_TYPE_cookie, |
407 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST |
408 | | | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY, |
409 | | OSSL_ECH_HANDLING_COMPRESS, |
410 | | NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie, |
411 | | tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL }, |
412 | | { /* |
413 | | * Special unsolicited ServerHello extension only used when |
414 | | * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but |
415 | | * ignore it. |
416 | | */ |
417 | | TLSEXT_TYPE_cryptopro_bug, |
418 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO |
419 | | | SSL_EXT_TLS1_2_AND_BELOW_ONLY, |
420 | | OSSL_ECH_HANDLING_COMPRESS, |
421 | | NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL }, |
422 | | { TLSEXT_TYPE_compress_certificate, |
423 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST |
424 | | | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY, |
425 | | OSSL_ECH_HANDLING_COMPRESS, |
426 | | tls_init_compress_certificate, |
427 | | tls_parse_compress_certificate, tls_parse_compress_certificate, |
428 | | tls_construct_compress_certificate, tls_construct_compress_certificate, |
429 | | NULL }, |
430 | | { TLSEXT_TYPE_early_data, |
431 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS |
432 | | | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY, |
433 | | OSSL_ECH_HANDLING_CALL_BOTH, |
434 | | NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data, |
435 | | tls_construct_stoc_early_data, tls_construct_ctos_early_data, |
436 | | final_early_data }, |
437 | | { |
438 | | TLSEXT_TYPE_certificate_authorities, |
439 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST |
440 | | | SSL_EXT_TLS1_3_ONLY, |
441 | | OSSL_ECH_HANDLING_COMPRESS, |
442 | | init_certificate_authorities, |
443 | | tls_parse_certificate_authorities, |
444 | | tls_parse_certificate_authorities, |
445 | | tls_construct_certificate_authorities, |
446 | | tls_construct_certificate_authorities, |
447 | | NULL, |
448 | | }, |
449 | | #ifndef OPENSSL_NO_ECH |
450 | | { TLSEXT_TYPE_ech, |
451 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
452 | | OSSL_ECH_HANDLING_CALL_BOTH, |
453 | | init_ech, |
454 | | tls_parse_ctos_ech, tls_parse_stoc_ech, |
455 | | tls_construct_stoc_ech, tls_construct_ctos_ech, |
456 | | final_ech }, |
457 | | { TLSEXT_TYPE_outer_extensions, |
458 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY, |
459 | | OSSL_ECH_HANDLING_CALL_BOTH, |
460 | | NULL, |
461 | | NULL, NULL, |
462 | | NULL, NULL, |
463 | | NULL }, |
464 | | #else /* OPENSSL_NO_ECH */ |
465 | | INVALID_EXTENSION, |
466 | | INVALID_EXTENSION, |
467 | | #endif /* END_OPENSSL_NO_ECH */ |
468 | | { /* RFC 8701 GREASE extension 1 - type is dynamic */ |
469 | | TLSEXT_TYPE_grease1, |
470 | | SSL_EXT_CLIENT_HELLO, |
471 | | 0, |
472 | | NULL, |
473 | | NULL, NULL, NULL, tls_construct_ctos_grease1, NULL }, |
474 | | { /* RFC 8701 GREASE extension 2 - type is dynamic */ |
475 | | TLSEXT_TYPE_grease2, |
476 | | SSL_EXT_CLIENT_HELLO, |
477 | | 0, |
478 | | NULL, |
479 | | NULL, NULL, NULL, tls_construct_ctos_grease2, NULL }, |
480 | | { /* Must be immediately before pre_shared_key */ |
481 | | TLSEXT_TYPE_padding, |
482 | | SSL_EXT_CLIENT_HELLO, |
483 | | OSSL_ECH_HANDLING_CALL_BOTH, |
484 | | NULL, |
485 | | /* We send this, but don't read it */ |
486 | | NULL, NULL, NULL, tls_construct_ctos_padding, NULL }, |
487 | | { /* Required by the TLSv1.3 spec to always be the last extension */ |
488 | | TLSEXT_TYPE_psk, |
489 | | SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO |
490 | | | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY, |
491 | | OSSL_ECH_HANDLING_CALL_BOTH, |
492 | | NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk, |
493 | | tls_construct_ctos_psk, final_psk } |
494 | | }; |
495 | | |
496 | | #ifndef OPENSSL_NO_ECH |
497 | | /* |
498 | | * Copy an inner extension value to outer. |
499 | | * inner CH must have been pre-decoded into s->clienthello->pre_proc_exts |
500 | | * already. |
501 | | */ |
502 | | int ossl_ech_copy_inner2outer(SSL_CONNECTION *s, uint16_t ext_type, |
503 | | int ind, WPACKET *pkt) |
504 | 0 | { |
505 | 0 | RAW_EXTENSION *myext = NULL, *raws = NULL; |
506 | |
|
507 | 0 | if (s == NULL || s->clienthello == NULL) |
508 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
509 | 0 | raws = s->clienthello->pre_proc_exts; |
510 | 0 | if (raws == NULL) |
511 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
512 | 0 | myext = &raws[ind]; |
513 | 0 | OSSL_TRACE_BEGIN(TLS) |
514 | 0 | { |
515 | 0 | BIO_printf(trc_out, "inner2outer: Copying ext type %d to outer\n", |
516 | 0 | ext_type); |
517 | 0 | } |
518 | 0 | OSSL_TRACE_END(TLS); |
519 | | |
520 | | /* |
521 | | * copy inner value to outer |
522 | | */ |
523 | 0 | if (PACKET_data(&myext->data) != NULL |
524 | 0 | && PACKET_remaining(&myext->data) > 0) { |
525 | 0 | if (!WPACKET_put_bytes_u16(pkt, ext_type) |
526 | 0 | || !WPACKET_sub_memcpy_u16(pkt, PACKET_data(&myext->data), |
527 | 0 | PACKET_remaining(&myext->data))) |
528 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
529 | 0 | } else { |
530 | | /* empty extension */ |
531 | 0 | if (!WPACKET_put_bytes_u16(pkt, ext_type) |
532 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) |
533 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
534 | 0 | } |
535 | 0 | return 1; |
536 | 0 | } |
537 | | |
538 | | /* |
539 | | * DUPEMALL is useful for testing - this turns off compression and |
540 | | * causes two calls to each extension constructor, which'd be the same |
541 | | * as making all entries in ext_tab use the CALL_BOTH value |
542 | | */ |
543 | | #undef DUPEMALL |
544 | | |
545 | | /* |
546 | | * Check if we're using the same/different key shares |
547 | | * return 1 if same key share in inner and outer, 0 otherwise |
548 | | */ |
549 | | int ossl_ech_same_key_share(void) |
550 | 0 | { |
551 | | #ifdef DUPEMALL |
552 | | return 0; |
553 | | #endif |
554 | 0 | return ext_defs[TLSEXT_IDX_key_share].ech_handling |
555 | 0 | != OSSL_ECH_HANDLING_CALL_BOTH; |
556 | 0 | } |
557 | | |
558 | | /* |
559 | | * say if extension at index |ind| in ext_defs is to be ECH compressed |
560 | | * return 1 if this one is to be compressed, 0 if not, -1 for error |
561 | | */ |
562 | | int ossl_ech_2bcompressed(size_t ind) |
563 | 0 | { |
564 | 0 | const size_t nexts = OSSL_NELEM(ext_defs); |
565 | |
|
566 | | #ifdef DUPEMALL |
567 | | return 0; |
568 | | #endif |
569 | 0 | if (ind >= nexts) |
570 | 0 | return -1; |
571 | 0 | return ext_defs[ind].ech_handling == OSSL_ECH_HANDLING_COMPRESS; |
572 | 0 | } |
573 | | |
574 | | /* as needed, repeat extension from inner in outer handling compression */ |
575 | | int ossl_ech_same_ext(SSL_CONNECTION *s, WPACKET *pkt) |
576 | 0 | { |
577 | 0 | unsigned int type = 0; |
578 | 0 | int tind = 0, nexts = OSSL_NELEM(ext_defs); |
579 | |
|
580 | | #ifdef DUPEMALL |
581 | | return OSSL_ECH_SAME_EXT_CONTINUE; |
582 | | #endif |
583 | 0 | if (s == NULL || s->ext.ech.es == NULL) |
584 | 0 | return OSSL_ECH_SAME_EXT_CONTINUE; /* nothing to do */ |
585 | | /* |
586 | | * We store/access the index of the extension handler in |
587 | | * s->ext.ech.ext_ind, as we'd otherwise not know it here. |
588 | | * Be nice were there a better way to handle that. |
589 | | */ |
590 | 0 | tind = s->ext.ech.ext_ind; |
591 | | /* If this index'd extension won't be compressed, we're done */ |
592 | 0 | if (tind < 0 || tind >= nexts) |
593 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
594 | 0 | type = ext_defs[tind].type; |
595 | 0 | if (s->ext.ech.ch_depth == 1) { |
596 | | /* inner CH - just note compression as configured */ |
597 | 0 | if (ext_defs[tind].ech_handling != OSSL_ECH_HANDLING_COMPRESS) |
598 | 0 | return OSSL_ECH_SAME_EXT_CONTINUE; |
599 | | /* mark this one to be "compressed" */ |
600 | 0 | if (s->ext.ech.n_outer_only >= OSSL_ECH_OUTERS_MAX) |
601 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
602 | 0 | s->ext.ech.outer_only[s->ext.ech.n_outer_only] = type; |
603 | 0 | s->ext.ech.n_outer_only++; |
604 | 0 | OSSL_TRACE_BEGIN(TLS) |
605 | 0 | { |
606 | 0 | BIO_printf(trc_out, "ech_same_ext: Marking (type %u, ind %d " |
607 | 0 | "tot-comp %d) for compression\n", |
608 | 0 | type, tind, |
609 | 0 | (int)s->ext.ech.n_outer_only); |
610 | 0 | } |
611 | 0 | OSSL_TRACE_END(TLS); |
612 | 0 | return OSSL_ECH_SAME_EXT_CONTINUE; |
613 | 0 | } else { |
614 | | /* Copy value from inner to outer, or indicate a new value needed */ |
615 | 0 | if (s->clienthello == NULL || pkt == NULL) |
616 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
617 | 0 | if (ext_defs[tind].ech_handling == OSSL_ECH_HANDLING_CALL_BOTH) |
618 | 0 | return OSSL_ECH_SAME_EXT_CONTINUE; |
619 | 0 | else |
620 | 0 | return ossl_ech_copy_inner2outer(s, type, tind, pkt); |
621 | 0 | } |
622 | | /* just in case - shouldn't happen */ |
623 | 0 | return OSSL_ECH_SAME_EXT_ERR; |
624 | 0 | } |
625 | | #endif |
626 | | |
627 | | /* Returns a TLSEXT_TYPE for the given index */ |
628 | | unsigned int ossl_get_extension_type(size_t idx) |
629 | 0 | { |
630 | 0 | size_t num_exts = OSSL_NELEM(ext_defs); |
631 | |
|
632 | 0 | if (idx >= num_exts) |
633 | 0 | return TLSEXT_TYPE_out_of_range; |
634 | | |
635 | 0 | return ext_defs[idx].type; |
636 | 0 | } |
637 | | |
638 | | /* Check whether an extension's context matches the current context */ |
639 | | static int validate_context(SSL_CONNECTION *s, unsigned int extctx, |
640 | | unsigned int thisctx) |
641 | 0 | { |
642 | | /* Check we're allowed to use this extension in this context */ |
643 | 0 | if ((thisctx & extctx) == 0) |
644 | 0 | return 0; |
645 | | |
646 | 0 | if (SSL_CONNECTION_IS_DTLS(s)) { |
647 | 0 | if ((extctx & SSL_EXT_TLS_ONLY) != 0) |
648 | 0 | return 0; |
649 | 0 | } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) { |
650 | 0 | return 0; |
651 | 0 | } |
652 | | |
653 | 0 | return 1; |
654 | 0 | } |
655 | | |
656 | | int tls_validate_all_contexts(SSL_CONNECTION *s, unsigned int thisctx, |
657 | | RAW_EXTENSION *exts) |
658 | 0 | { |
659 | 0 | size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset; |
660 | 0 | RAW_EXTENSION *thisext; |
661 | 0 | unsigned int context; |
662 | 0 | ENDPOINT role = ENDPOINT_BOTH; |
663 | |
|
664 | 0 | if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0) |
665 | 0 | role = ENDPOINT_SERVER; |
666 | 0 | else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0) |
667 | 0 | role = ENDPOINT_CLIENT; |
668 | | |
669 | | /* Calculate the number of extensions in the extensions list */ |
670 | 0 | num_exts = builtin_num + s->cert->custext.meths_count; |
671 | |
|
672 | 0 | for (thisext = exts, i = 0; i < num_exts; i++, thisext++) { |
673 | 0 | if (!thisext->present) |
674 | 0 | continue; |
675 | | |
676 | 0 | if (i < builtin_num) { |
677 | 0 | context = ext_defs[i].context; |
678 | 0 | } else { |
679 | 0 | custom_ext_method *meth = NULL; |
680 | |
|
681 | 0 | meth = custom_ext_find(&s->cert->custext, role, thisext->type, |
682 | 0 | &offset); |
683 | 0 | if (!ossl_assert(meth != NULL)) |
684 | 0 | return 0; |
685 | 0 | context = meth->context; |
686 | 0 | } |
687 | | |
688 | 0 | if (!validate_context(s, context, thisctx)) |
689 | 0 | return 0; |
690 | 0 | } |
691 | | |
692 | 0 | return 1; |
693 | 0 | } |
694 | | |
695 | | /* |
696 | | * Verify whether we are allowed to use the extension |type| in the current |
697 | | * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to |
698 | | * indicate the extension is not allowed. If returning 1 then |*found| is set to |
699 | | * the definition for the extension we found. |
700 | | */ |
701 | | static int verify_extension(SSL_CONNECTION *s, unsigned int context, |
702 | | unsigned int type, custom_ext_methods *meths, |
703 | | RAW_EXTENSION *rawexlist, RAW_EXTENSION **found) |
704 | 0 | { |
705 | 0 | size_t i; |
706 | 0 | size_t builtin_num = OSSL_NELEM(ext_defs); |
707 | 0 | const EXTENSION_DEFINITION *thisext; |
708 | |
|
709 | 0 | for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) { |
710 | 0 | if (type == thisext->type) { |
711 | 0 | if (!validate_context(s, thisext->context, context)) |
712 | 0 | return 0; |
713 | | |
714 | 0 | *found = &rawexlist[i]; |
715 | 0 | return 1; |
716 | 0 | } |
717 | 0 | } |
718 | | |
719 | | /* Check the custom extensions */ |
720 | 0 | if (meths != NULL) { |
721 | 0 | size_t offset = 0; |
722 | 0 | ENDPOINT role = ENDPOINT_BOTH; |
723 | 0 | custom_ext_method *meth = NULL; |
724 | |
|
725 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0) { |
726 | 0 | #ifndef OPENSSL_NO_ECH |
727 | 0 | if (s->ext.ech.attempted == 1 && s->ext.ech.ch_depth == 1) |
728 | 0 | role = ENDPOINT_CLIENT; |
729 | 0 | else |
730 | 0 | role = ENDPOINT_SERVER; |
731 | | #else |
732 | | role = ENDPOINT_SERVER; |
733 | | #endif |
734 | 0 | } else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0) |
735 | 0 | role = ENDPOINT_CLIENT; |
736 | |
|
737 | 0 | meth = custom_ext_find(meths, role, type, &offset); |
738 | 0 | if (meth != NULL) { |
739 | 0 | if (!validate_context(s, meth->context, context)) |
740 | 0 | return 0; |
741 | 0 | *found = &rawexlist[offset + builtin_num]; |
742 | 0 | return 1; |
743 | 0 | } |
744 | 0 | } |
745 | | |
746 | | /* Unknown extension. We allow it */ |
747 | 0 | *found = NULL; |
748 | 0 | return 1; |
749 | 0 | } |
750 | | |
751 | | /* |
752 | | * Check whether the context defined for an extension |extctx| means whether |
753 | | * the extension is relevant for the current context |thisctx| or not. Returns |
754 | | * 1 if the extension is relevant for this context, and 0 otherwise |
755 | | */ |
756 | | int extension_is_relevant(SSL_CONNECTION *s, unsigned int extctx, |
757 | | unsigned int thisctx) |
758 | 0 | { |
759 | 0 | int is_tls13; |
760 | | |
761 | | /* |
762 | | * For HRR we haven't selected the version yet but we know it will be |
763 | | * TLSv1.3 |
764 | | */ |
765 | 0 | if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) |
766 | 0 | is_tls13 = 1; |
767 | 0 | else |
768 | 0 | is_tls13 = SSL_CONNECTION_IS_TLS13(s); |
769 | |
|
770 | 0 | if ((SSL_CONNECTION_IS_DTLS(s) |
771 | 0 | && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0) |
772 | | /* |
773 | | * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated", |
774 | | * which is never true when generating the ClientHello. |
775 | | * However, version negotiation *has* occurred by the time the |
776 | | * ClientHello extensions are being parsed. |
777 | | * Be careful to allow TLS 1.3-only extensions when generating |
778 | | * the ClientHello. |
779 | | */ |
780 | 0 | || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0) |
781 | 0 | || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0 |
782 | 0 | && (thisctx & SSL_EXT_CLIENT_HELLO) == 0) |
783 | 0 | || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0) |
784 | 0 | || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0)) |
785 | 0 | return 0; |
786 | 0 | return 1; |
787 | 0 | } |
788 | | |
789 | | /* |
790 | | * Gather a list of all the extensions from the data in |packet]. |context| |
791 | | * tells us which message this extension is for. The raw extension data is |
792 | | * stored in |*res| on success. We don't actually process the content of the |
793 | | * extensions yet, except to check their types. This function also runs the |
794 | | * initialiser functions for all known extensions if |init| is nonzero (whether |
795 | | * we have collected them or not). If successful the caller is responsible for |
796 | | * freeing the contents of |*res|. |
797 | | * |
798 | | * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
799 | | * more than one extension of the same type in a ClientHello or ServerHello. |
800 | | * This function returns 1 if all extensions are unique and we have parsed their |
801 | | * types, and 0 if the extensions contain duplicates, could not be successfully |
802 | | * found, or an internal error occurred. We only check duplicates for |
803 | | * extensions that we know about. We ignore others. |
804 | | */ |
805 | | int tls_collect_extensions(SSL_CONNECTION *s, PACKET *packet, |
806 | | unsigned int context, |
807 | | RAW_EXTENSION **res, size_t *len, int init) |
808 | 0 | { |
809 | 0 | PACKET extensions = *packet; |
810 | 0 | size_t i = 0; |
811 | 0 | size_t num_exts; |
812 | 0 | custom_ext_methods *exts = &s->cert->custext; |
813 | 0 | RAW_EXTENSION *raw_extensions = NULL; |
814 | 0 | const EXTENSION_DEFINITION *thisexd; |
815 | |
|
816 | 0 | *res = NULL; |
817 | | |
818 | | /* |
819 | | * Initialise server side custom extensions. Client side is done during |
820 | | * construction of extensions for the ClientHello. |
821 | | */ |
822 | 0 | #ifndef OPENSSL_NO_ECH |
823 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0 && s->ext.ech.attempted == 0) |
824 | 0 | custom_ext_init(&s->cert->custext); |
825 | | #else |
826 | | if ((context & SSL_EXT_CLIENT_HELLO) != 0) |
827 | | custom_ext_init(&s->cert->custext); |
828 | | #endif |
829 | |
|
830 | 0 | num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0); |
831 | 0 | raw_extensions = OPENSSL_calloc(num_exts, sizeof(*raw_extensions)); |
832 | 0 | if (raw_extensions == NULL) { |
833 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
834 | 0 | return 0; |
835 | 0 | } |
836 | | |
837 | 0 | i = 0; |
838 | 0 | while (PACKET_remaining(&extensions) > 0) { |
839 | 0 | unsigned int type, idx; |
840 | 0 | PACKET extension; |
841 | 0 | RAW_EXTENSION *thisex; |
842 | |
|
843 | 0 | if (!PACKET_get_net_2(&extensions, &type) || !PACKET_get_length_prefixed_2(&extensions, &extension)) { |
844 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
845 | 0 | goto err; |
846 | 0 | } |
847 | | /* |
848 | | * Verify this extension is allowed. We only check duplicates for |
849 | | * extensions that we recognise. We also have a special case for the |
850 | | * PSK extension, which must be the last one in the ClientHello. |
851 | | */ |
852 | 0 | if (!verify_extension(s, context, type, exts, raw_extensions, &thisex) |
853 | 0 | || (thisex != NULL && thisex->present == 1) |
854 | 0 | || (type == TLSEXT_TYPE_psk |
855 | 0 | && (context & SSL_EXT_CLIENT_HELLO) != 0 |
856 | 0 | && PACKET_remaining(&extensions) != 0)) { |
857 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
858 | 0 | goto err; |
859 | 0 | } |
860 | 0 | idx = (unsigned int)(thisex - raw_extensions); |
861 | | /*- |
862 | | * Check that we requested this extension (if appropriate). Requests can |
863 | | * be sent in the ClientHello and CertificateRequest. Unsolicited |
864 | | * extensions can be sent in the NewSessionTicket. We only do this for |
865 | | * the built-in extensions. Custom extensions have a different but |
866 | | * similar check elsewhere. |
867 | | * Special cases: |
868 | | * - The HRR cookie extension is unsolicited |
869 | | * - The renegotiate extension is unsolicited (the client signals |
870 | | * support via an SCSV) |
871 | | * - The signed_certificate_timestamp extension can be provided by a |
872 | | * custom extension or by the built-in version. We let the extension |
873 | | * itself handle unsolicited response checks. |
874 | | */ |
875 | 0 | if (idx < OSSL_NELEM(ext_defs) |
876 | 0 | && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0 |
877 | 0 | && type != TLSEXT_TYPE_cookie |
878 | 0 | && type != TLSEXT_TYPE_renegotiate |
879 | 0 | && type != TLSEXT_TYPE_signed_certificate_timestamp |
880 | 0 | && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0 |
881 | 0 | #ifndef OPENSSL_NO_GOST |
882 | 0 | && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0 |
883 | 0 | && type == TLSEXT_TYPE_cryptopro_bug) |
884 | 0 | #endif |
885 | 0 | ) { |
886 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, |
887 | 0 | SSL_R_UNSOLICITED_EXTENSION); |
888 | 0 | goto err; |
889 | 0 | } |
890 | 0 | if (thisex != NULL) { |
891 | 0 | thisex->data = extension; |
892 | 0 | thisex->present = 1; |
893 | 0 | thisex->type = type; |
894 | 0 | thisex->received_order = i++; |
895 | 0 | if (s->ext.debug_cb) |
896 | 0 | s->ext.debug_cb(SSL_CONNECTION_GET_USER_SSL(s), !s->server, |
897 | 0 | thisex->type, PACKET_data(&thisex->data), |
898 | 0 | (int)PACKET_remaining(&thisex->data), |
899 | 0 | s->ext.debug_arg); |
900 | 0 | } |
901 | 0 | } |
902 | | |
903 | 0 | if (init) { |
904 | | /* |
905 | | * Initialise all known extensions relevant to this context, |
906 | | * whether we have found them or not |
907 | | */ |
908 | 0 | for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs); |
909 | 0 | i++, thisexd++) { |
910 | 0 | if (thisexd->init != NULL && (thisexd->context & context) != 0 |
911 | 0 | && extension_is_relevant(s, thisexd->context, context) |
912 | 0 | && !thisexd->init(s, context)) { |
913 | | /* SSLfatal() already called */ |
914 | 0 | goto err; |
915 | 0 | } |
916 | 0 | } |
917 | 0 | } |
918 | | |
919 | 0 | *res = raw_extensions; |
920 | 0 | if (len != NULL) |
921 | 0 | *len = num_exts; |
922 | 0 | return 1; |
923 | | |
924 | 0 | err: |
925 | 0 | OPENSSL_free(raw_extensions); |
926 | 0 | return 0; |
927 | 0 | } |
928 | | |
929 | | /* |
930 | | * Runs the parser for a given extension with index |idx|. |exts| contains the |
931 | | * list of all parsed extensions previously collected by |
932 | | * tls_collect_extensions(). The parser is only run if it is applicable for the |
933 | | * given |context| and the parser has not already been run. If this is for a |
934 | | * Certificate message, then we also provide the parser with the relevant |
935 | | * Certificate |x| and its position in the |chainidx| with 0 being the first |
936 | | * Certificate. Returns 1 on success or 0 on failure. If an extension is not |
937 | | * present this counted as success. |
938 | | */ |
939 | | int tls_parse_extension(SSL_CONNECTION *s, TLSEXT_INDEX idx, int context, |
940 | | RAW_EXTENSION *exts, X509 *x, size_t chainidx) |
941 | 0 | { |
942 | 0 | RAW_EXTENSION *currext = &exts[idx]; |
943 | 0 | int (*parser)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, X509 *x, |
944 | 0 | size_t chainidx) |
945 | 0 | = NULL; |
946 | | |
947 | | /* Skip if the extension is not present */ |
948 | 0 | if (!currext->present) |
949 | 0 | return 1; |
950 | | |
951 | | /* Skip if we've already parsed this extension */ |
952 | 0 | if (currext->parsed) |
953 | 0 | return 1; |
954 | | |
955 | 0 | currext->parsed = 1; |
956 | |
|
957 | 0 | if (idx < OSSL_NELEM(ext_defs)) { |
958 | | /* We are handling a built-in extension */ |
959 | 0 | const EXTENSION_DEFINITION *extdef = &ext_defs[idx]; |
960 | | |
961 | | /* Check if extension is defined for our protocol. If not, skip */ |
962 | 0 | if (!extension_is_relevant(s, extdef->context, context)) |
963 | 0 | return 1; |
964 | | |
965 | 0 | parser = s->server ? extdef->parse_ctos : extdef->parse_stoc; |
966 | |
|
967 | 0 | if (parser != NULL) |
968 | 0 | return parser(s, &currext->data, context, x, chainidx); |
969 | | |
970 | | /* |
971 | | * If the parser is NULL we fall through to the custom extension |
972 | | * processing |
973 | | */ |
974 | 0 | } |
975 | | |
976 | | /* Parse custom extensions */ |
977 | 0 | return custom_ext_parse(s, context, currext->type, |
978 | 0 | PACKET_data(&currext->data), |
979 | 0 | PACKET_remaining(&currext->data), |
980 | 0 | x, chainidx); |
981 | 0 | } |
982 | | |
983 | | /* |
984 | | * Parse all remaining extensions that have not yet been parsed. Also calls the |
985 | | * finalisation for all extensions at the end if |fin| is nonzero, whether we |
986 | | * collected them or not. Returns 1 for success or 0 for failure. If we are |
987 | | * working on a Certificate message then we also pass the Certificate |x| and |
988 | | * its position in the |chainidx|, with 0 being the first certificate. |
989 | | */ |
990 | | int tls_parse_all_extensions(SSL_CONNECTION *s, int context, |
991 | | RAW_EXTENSION *exts, X509 *x, |
992 | | size_t chainidx, int fin) |
993 | 0 | { |
994 | 0 | size_t i, numexts = OSSL_NELEM(ext_defs); |
995 | 0 | const EXTENSION_DEFINITION *thisexd; |
996 | | |
997 | | /* Calculate the number of extensions in the extensions list */ |
998 | 0 | numexts += s->cert->custext.meths_count; |
999 | | |
1000 | | /* Parse each extension in turn */ |
1001 | 0 | for (i = 0; i < numexts; i++) { |
1002 | 0 | if (!tls_parse_extension(s, i, context, exts, x, chainidx)) { |
1003 | | /* SSLfatal() already called */ |
1004 | 0 | return 0; |
1005 | 0 | } |
1006 | 0 | } |
1007 | | |
1008 | 0 | if (fin) { |
1009 | | /* |
1010 | | * Finalise all known extensions relevant to this context, |
1011 | | * whether we have found them or not |
1012 | | */ |
1013 | 0 | for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); |
1014 | 0 | i++, thisexd++) { |
1015 | 0 | if (thisexd->final != NULL && (thisexd->context & context) != 0 |
1016 | 0 | && !thisexd->final(s, context, exts[i].present)) { |
1017 | | /* SSLfatal() already called */ |
1018 | 0 | return 0; |
1019 | 0 | } |
1020 | 0 | } |
1021 | 0 | } |
1022 | | |
1023 | 0 | return 1; |
1024 | 0 | } |
1025 | | |
1026 | | int should_add_extension(SSL_CONNECTION *s, unsigned int extctx, |
1027 | | unsigned int thisctx, int max_version) |
1028 | 0 | { |
1029 | | /* Skip if not relevant for our context */ |
1030 | 0 | if ((extctx & thisctx) == 0) |
1031 | 0 | return 0; |
1032 | | |
1033 | | /* Check if this extension is defined for our protocol. If not, skip */ |
1034 | 0 | if (!extension_is_relevant(s, extctx, thisctx) |
1035 | 0 | || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0 |
1036 | 0 | && (thisctx & SSL_EXT_CLIENT_HELLO) != 0 |
1037 | 0 | && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION))) |
1038 | 0 | return 0; |
1039 | | |
1040 | 0 | return 1; |
1041 | 0 | } |
1042 | | |
1043 | | /* |
1044 | | * Construct all the extensions relevant to the current |context| and write |
1045 | | * them to |pkt|. If this is an extension for a Certificate in a Certificate |
1046 | | * message, then |x| will be set to the Certificate we are handling, and |
1047 | | * |chainidx| will indicate the position in the chainidx we are processing (with |
1048 | | * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a |
1049 | | * failure construction stops at the first extension to fail to construct. |
1050 | | */ |
1051 | | int tls_construct_extensions(SSL_CONNECTION *s, WPACKET *pkt, |
1052 | | unsigned int context, |
1053 | | X509 *x, size_t chainidx) |
1054 | 0 | { |
1055 | 0 | size_t i; |
1056 | 0 | int min_version, max_version = 0, reason; |
1057 | 0 | const EXTENSION_DEFINITION *thisexd; |
1058 | 0 | int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0; |
1059 | 0 | #ifndef OPENSSL_NO_ECH |
1060 | 0 | int pass; |
1061 | 0 | #endif |
1062 | |
|
1063 | 0 | if (!WPACKET_start_sub_packet_u16(pkt) |
1064 | | /* |
1065 | | * If extensions are of zero length then we don't even add the |
1066 | | * extensions length bytes to a ClientHello/ServerHello |
1067 | | * (for non-TLSv1.3). |
1068 | | */ |
1069 | 0 | || ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0 |
1070 | 0 | && !WPACKET_set_flags(pkt, |
1071 | 0 | WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) { |
1072 | 0 | if (!for_comp) |
1073 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1074 | 0 | return 0; |
1075 | 0 | } |
1076 | | |
1077 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0) { |
1078 | 0 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
1079 | 0 | if (reason != 0) { |
1080 | 0 | if (!for_comp) |
1081 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
1082 | 0 | return 0; |
1083 | 0 | } |
1084 | 0 | } |
1085 | | |
1086 | | /* Add custom extensions first */ |
1087 | 0 | #ifndef OPENSSL_NO_ECH |
1088 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0 && s->ext.ech.attempted == 0) |
1089 | | /* On the server side with initialise during ClientHello parsing */ |
1090 | 0 | custom_ext_init(&s->cert->custext); |
1091 | | #else |
1092 | | if ((context & SSL_EXT_CLIENT_HELLO) != 0) |
1093 | | /* On the server side with initialise during ClientHello parsing */ |
1094 | | custom_ext_init(&s->cert->custext); |
1095 | | #endif |
1096 | 0 | if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) { |
1097 | | /* SSLfatal() already called */ |
1098 | 0 | return 0; |
1099 | 0 | } |
1100 | | |
1101 | 0 | #ifndef OPENSSL_NO_ECH |
1102 | | /* |
1103 | | * Two passes if doing real ECH - we first construct the |
1104 | | * to-be-ECH-compressed extensions, and then go around again |
1105 | | * constructing those that aren't to be ECH-compressed. We |
1106 | | * need to ensure this ordering so that all the ECH-compressed |
1107 | | * extensions are contiguous in the encoding. The actual |
1108 | | * compression happens later in ech_encode_inner(). |
1109 | | */ |
1110 | 0 | for (pass = 0; pass <= 1; pass++) |
1111 | 0 | #endif |
1112 | | |
1113 | 0 | for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); |
1114 | 0 | i++, thisexd++) { |
1115 | 0 | EXT_RETURN (*construct)(SSL_CONNECTION *s, WPACKET *pkt, |
1116 | 0 | unsigned int context, |
1117 | 0 | X509 *x, size_t chainidx); |
1118 | 0 | EXT_RETURN ret; |
1119 | |
|
1120 | 0 | #ifndef OPENSSL_NO_ECH |
1121 | | /* do compressed in pass 0, non-compressed in pass 1 */ |
1122 | 0 | if (ossl_ech_2bcompressed((int)i) == pass) |
1123 | 0 | continue; |
1124 | | /* stash index - needed for COMPRESS ECH handling */ |
1125 | 0 | s->ext.ech.ext_ind = (int)i; |
1126 | 0 | #endif |
1127 | | /* Skip if not relevant for our context */ |
1128 | 0 | if (!should_add_extension(s, thisexd->context, context, max_version)) |
1129 | 0 | continue; |
1130 | | |
1131 | 0 | construct = s->server ? thisexd->construct_stoc |
1132 | 0 | : thisexd->construct_ctos; |
1133 | |
|
1134 | 0 | if (construct == NULL) |
1135 | 0 | continue; |
1136 | | |
1137 | 0 | ret = construct(s, pkt, context, x, chainidx); |
1138 | 0 | if (ret == EXT_RETURN_FAIL) { |
1139 | | /* SSLfatal() already called */ |
1140 | 0 | return 0; |
1141 | 0 | } |
1142 | 0 | if (ret == EXT_RETURN_SENT |
1143 | 0 | && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0) |
1144 | 0 | s->ext.extflags[i] |= SSL_EXT_FLAG_SENT; |
1145 | 0 | } |
1146 | | |
1147 | 0 | #ifndef OPENSSL_NO_ECH |
1148 | | /* |
1149 | | * don't close yet if client in the middle of doing ECH, we'll |
1150 | | * eventually close this in ech_aad_and_encrypt() after we add |
1151 | | * the real ECH extension value |
1152 | | */ |
1153 | 0 | if (s->server |
1154 | 0 | || context != SSL_EXT_CLIENT_HELLO |
1155 | 0 | || s->ext.ech.attempted == 0 |
1156 | 0 | || s->ext.ech.ch_depth == 1 |
1157 | 0 | || s->ext.ech.grease == OSSL_ECH_IS_GREASE) { |
1158 | 0 | if (!WPACKET_close(pkt)) { |
1159 | 0 | if (!for_comp) |
1160 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1161 | 0 | return 0; |
1162 | 0 | } |
1163 | 0 | } |
1164 | | #else |
1165 | | if (!WPACKET_close(pkt)) { |
1166 | | if (!for_comp) |
1167 | | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1168 | | return 0; |
1169 | | } |
1170 | | #endif |
1171 | 0 | return 1; |
1172 | 0 | } |
1173 | | |
1174 | | /* |
1175 | | * Built in extension finalisation and initialisation functions. All initialise |
1176 | | * or finalise the associated extension type for the given |context|. For |
1177 | | * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0 |
1178 | | * otherwise. These functions return 1 on success or 0 on failure. |
1179 | | */ |
1180 | | |
1181 | | static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent) |
1182 | 0 | { |
1183 | 0 | if (!s->server) { |
1184 | | /* |
1185 | | * Check if we can connect to a server that doesn't support safe |
1186 | | * renegotiation |
1187 | | */ |
1188 | 0 | if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT) |
1189 | 0 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) |
1190 | 0 | && !sent) { |
1191 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
1192 | 0 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
1193 | 0 | return 0; |
1194 | 0 | } |
1195 | | |
1196 | 0 | return 1; |
1197 | 0 | } |
1198 | | |
1199 | | /* Need RI if renegotiating */ |
1200 | 0 | if (s->renegotiate |
1201 | 0 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) |
1202 | 0 | && !sent) { |
1203 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
1204 | 0 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
1205 | 0 | return 0; |
1206 | 0 | } |
1207 | | |
1208 | 0 | return 1; |
1209 | 0 | } |
1210 | | |
1211 | | static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx, |
1212 | | TSAN_QUALIFIER int *stat) |
1213 | 0 | { |
1214 | 0 | if (ssl_tsan_lock(ctx)) { |
1215 | 0 | tsan_decr(stat); |
1216 | 0 | ssl_tsan_unlock(ctx); |
1217 | 0 | } |
1218 | 0 | } |
1219 | | |
1220 | | static int init_server_name(SSL_CONNECTION *s, unsigned int context) |
1221 | 0 | { |
1222 | 0 | if (s->server) { |
1223 | 0 | s->servername_done = 0; |
1224 | |
|
1225 | 0 | OPENSSL_free(s->ext.hostname); |
1226 | 0 | s->ext.hostname = NULL; |
1227 | 0 | } |
1228 | |
|
1229 | 0 | return 1; |
1230 | 0 | } |
1231 | | |
1232 | | #ifndef OPENSSL_NO_ECH |
1233 | | /* |
1234 | | * Just note that ech is not yet done |
1235 | | * return 1 for good, 0 otherwise |
1236 | | */ |
1237 | | static int init_ech(SSL_CONNECTION *s, unsigned int context) |
1238 | 0 | { |
1239 | 0 | const int nexts = OSSL_NELEM(ext_defs); |
1240 | | |
1241 | | /* we don't need this assert everywhere - anywhere is fine */ |
1242 | 0 | if (!ossl_assert(TLSEXT_IDX_num_builtins == nexts)) { |
1243 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1244 | 0 | return 0; |
1245 | 0 | } |
1246 | 0 | if ((context & SSL_EXT_CLIENT_HELLO) != 0) |
1247 | 0 | s->ext.ech.done = 0; |
1248 | 0 | return 1; |
1249 | 0 | } |
1250 | | |
1251 | | static int final_ech(SSL_CONNECTION *s, unsigned int context, int sent) |
1252 | 0 | { |
1253 | 0 | if (s->server && s->ext.ech.success == 1 |
1254 | 0 | && s->ext.ech.inner_ech_seen_ok != 1) { |
1255 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_ECH_REQUIRED); |
1256 | 0 | return 0; |
1257 | 0 | } |
1258 | 0 | return 1; |
1259 | 0 | } |
1260 | | #endif /* OPENSSL_NO_ECH */ |
1261 | | |
1262 | | static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent) |
1263 | 0 | { |
1264 | 0 | int ret = SSL_TLSEXT_ERR_NOACK; |
1265 | 0 | int altmp = SSL_AD_UNRECOGNIZED_NAME; |
1266 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1267 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1268 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1269 | 0 | int was_ticket = (SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0; |
1270 | |
|
1271 | 0 | if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) { |
1272 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1273 | 0 | return 0; |
1274 | 0 | } |
1275 | | |
1276 | 0 | if (sctx->ext.servername_cb != NULL) |
1277 | 0 | ret = sctx->ext.servername_cb(ussl, &altmp, |
1278 | 0 | sctx->ext.servername_arg); |
1279 | 0 | else if (s->session_ctx->ext.servername_cb != NULL) |
1280 | 0 | ret = s->session_ctx->ext.servername_cb(ussl, &altmp, |
1281 | 0 | s->session_ctx->ext.servername_arg); |
1282 | | |
1283 | | /* |
1284 | | * For servers, propagate the SNI hostname from the temporary |
1285 | | * storage in the SSL to the persistent SSL_SESSION, now that we |
1286 | | * know we accepted it. |
1287 | | * Clients make this copy when parsing the server's response to |
1288 | | * the extension, which is when they find out that the negotiation |
1289 | | * was successful. |
1290 | | */ |
1291 | 0 | if (s->server) { |
1292 | 0 | if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) { |
1293 | | /* Only store the hostname in the session if we accepted it. */ |
1294 | 0 | OPENSSL_free(s->session->ext.hostname); |
1295 | 0 | s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname); |
1296 | 0 | if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) { |
1297 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1298 | 0 | } |
1299 | 0 | } |
1300 | 0 | } |
1301 | | |
1302 | | /* |
1303 | | * If we switched contexts (whether here or in the client_hello callback), |
1304 | | * move the sess_accept increment from the session_ctx to the new |
1305 | | * context, to avoid the confusing situation of having sess_accept_good |
1306 | | * exceed sess_accept (zero) for the new context. |
1307 | | */ |
1308 | 0 | if (SSL_IS_FIRST_HANDSHAKE(s) && sctx != s->session_ctx |
1309 | 0 | && s->hello_retry_request == SSL_HRR_NONE) { |
1310 | 0 | ssl_tsan_counter(sctx, &sctx->stats.sess_accept); |
1311 | 0 | ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept); |
1312 | 0 | } |
1313 | | |
1314 | | /* |
1315 | | * If we're expecting to send a ticket, and tickets were previously enabled, |
1316 | | * and now tickets are disabled, then turn off expected ticket. |
1317 | | * Also, if this is not a resumption, create a new session ID |
1318 | | */ |
1319 | 0 | if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected |
1320 | 0 | && was_ticket && (SSL_get_options(ssl) & SSL_OP_NO_TICKET) != 0) { |
1321 | 0 | s->ext.ticket_expected = 0; |
1322 | 0 | if (!s->hit) { |
1323 | 0 | SSL_SESSION *ss = SSL_get_session(ssl); |
1324 | |
|
1325 | 0 | if (ss != NULL) { |
1326 | 0 | OPENSSL_free(ss->ext.tick); |
1327 | 0 | ss->ext.tick = NULL; |
1328 | 0 | ss->ext.ticklen = 0; |
1329 | 0 | ss->ext.tick_lifetime_hint = 0; |
1330 | 0 | ss->ext.tick_age_add = 0; |
1331 | 0 | if (!ssl_generate_session_id(s, ss)) { |
1332 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1333 | 0 | return 0; |
1334 | 0 | } |
1335 | 0 | } else { |
1336 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1337 | 0 | return 0; |
1338 | 0 | } |
1339 | 0 | } |
1340 | 0 | } |
1341 | | |
1342 | 0 | switch (ret) { |
1343 | 0 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
1344 | 0 | SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED); |
1345 | 0 | return 0; |
1346 | | |
1347 | 0 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
1348 | | /* TLSv1.3 doesn't have warning alerts so we suppress this */ |
1349 | 0 | if (!SSL_CONNECTION_IS_TLS13(s)) |
1350 | 0 | ssl3_send_alert(s, SSL3_AL_WARNING, altmp); |
1351 | 0 | s->servername_done = 0; |
1352 | 0 | return 1; |
1353 | | |
1354 | 0 | case SSL_TLSEXT_ERR_NOACK: |
1355 | 0 | s->servername_done = 0; |
1356 | 0 | return 1; |
1357 | | |
1358 | 0 | default: |
1359 | 0 | return 1; |
1360 | 0 | } |
1361 | 0 | } |
1362 | | |
1363 | | static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context, |
1364 | | int sent) |
1365 | 0 | { |
1366 | 0 | unsigned long alg_k, alg_a; |
1367 | |
|
1368 | 0 | if (s->server) |
1369 | 0 | return 1; |
1370 | | |
1371 | 0 | alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
1372 | 0 | alg_a = s->s3.tmp.new_cipher->algorithm_auth; |
1373 | | |
1374 | | /* |
1375 | | * If we are client and using an elliptic curve cryptography cipher |
1376 | | * suite, then if server returns an EC point formats lists extension it |
1377 | | * must contain uncompressed. |
1378 | | */ |
1379 | 0 | if (s->ext.ecpointformats != NULL |
1380 | 0 | && s->ext.ecpointformats_len > 0 |
1381 | 0 | && s->ext.peer_ecpointformats != NULL |
1382 | 0 | && s->ext.peer_ecpointformats_len > 0 |
1383 | 0 | && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { |
1384 | | /* we are using an ECC cipher */ |
1385 | 0 | size_t i; |
1386 | 0 | unsigned char *list = s->ext.peer_ecpointformats; |
1387 | |
|
1388 | 0 | for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { |
1389 | 0 | if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed) |
1390 | 0 | break; |
1391 | 0 | } |
1392 | 0 | if (i == s->ext.peer_ecpointformats_len) { |
1393 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1394 | 0 | SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
1395 | 0 | return 0; |
1396 | 0 | } |
1397 | 0 | } |
1398 | | |
1399 | 0 | return 1; |
1400 | 0 | } |
1401 | | |
1402 | | static int init_session_ticket(SSL_CONNECTION *s, unsigned int context) |
1403 | 0 | { |
1404 | 0 | if (!s->server) |
1405 | 0 | s->ext.ticket_expected = 0; |
1406 | |
|
1407 | 0 | return 1; |
1408 | 0 | } |
1409 | | |
1410 | | #ifndef OPENSSL_NO_OCSP |
1411 | | static int init_status_request(SSL_CONNECTION *s, unsigned int context) |
1412 | 0 | { |
1413 | 0 | if (s->server) { |
1414 | 0 | s->ext.status_type = TLSEXT_STATUSTYPE_nothing; |
1415 | 0 | } else { |
1416 | | /* |
1417 | | * Ensure we get sensible values passed to tlsext_status_cb in the event |
1418 | | * that we don't receive a status message |
1419 | | */ |
1420 | 0 | OPENSSL_free(s->ext.ocsp.resp); |
1421 | 0 | s->ext.ocsp.resp = NULL; |
1422 | 0 | s->ext.ocsp.resp_len = 0; |
1423 | |
|
1424 | 0 | sk_OCSP_RESPONSE_pop_free(s->ext.ocsp.resp_ex, OCSP_RESPONSE_free); |
1425 | 0 | s->ext.ocsp.resp_ex = NULL; |
1426 | 0 | } |
1427 | |
|
1428 | 0 | return 1; |
1429 | 0 | } |
1430 | | #endif |
1431 | | |
1432 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
1433 | | static int init_npn(SSL_CONNECTION *s, unsigned int context) |
1434 | 0 | { |
1435 | 0 | s->s3.npn_seen = 0; |
1436 | |
|
1437 | 0 | return 1; |
1438 | 0 | } |
1439 | | #endif |
1440 | | |
1441 | | static int init_alpn(SSL_CONNECTION *s, unsigned int context) |
1442 | 0 | { |
1443 | 0 | OPENSSL_free(s->s3.alpn_selected); |
1444 | 0 | s->s3.alpn_selected = NULL; |
1445 | 0 | s->s3.alpn_selected_len = 0; |
1446 | 0 | if (s->server) { |
1447 | 0 | OPENSSL_free(s->s3.alpn_proposed); |
1448 | 0 | s->s3.alpn_proposed = NULL; |
1449 | 0 | s->s3.alpn_proposed_len = 0; |
1450 | 0 | } |
1451 | 0 | return 1; |
1452 | 0 | } |
1453 | | |
1454 | | static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent) |
1455 | 0 | { |
1456 | 0 | if (!s->server && !sent && s->session->ext.alpn_selected != NULL) |
1457 | 0 | s->ext.early_data_ok = 0; |
1458 | |
|
1459 | 0 | if (!s->server || !SSL_CONNECTION_IS_TLS13(s)) |
1460 | 0 | return 1; |
1461 | | |
1462 | | /* |
1463 | | * Call alpn_select callback if needed. Has to be done after SNI and |
1464 | | * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3 |
1465 | | * we also have to do this before we decide whether to accept early_data. |
1466 | | * In TLSv1.3 we've already negotiated our cipher so we do this call now. |
1467 | | * For < TLSv1.3 we defer it until after cipher negotiation. |
1468 | | * |
1469 | | * On failure SSLfatal() already called. |
1470 | | */ |
1471 | 0 | return tls_handle_alpn(s); |
1472 | 0 | } |
1473 | | |
1474 | | static int init_sig_algs(SSL_CONNECTION *s, unsigned int context) |
1475 | 0 | { |
1476 | | /* Clear any signature algorithms extension received */ |
1477 | 0 | OPENSSL_free(s->s3.tmp.peer_sigalgs); |
1478 | 0 | s->s3.tmp.peer_sigalgs = NULL; |
1479 | 0 | s->s3.tmp.peer_sigalgslen = 0; |
1480 | |
|
1481 | 0 | return 1; |
1482 | 0 | } |
1483 | | |
1484 | | static int init_sig_algs_cert(SSL_CONNECTION *s, |
1485 | | ossl_unused unsigned int context) |
1486 | 0 | { |
1487 | | /* Clear any signature algorithms extension received */ |
1488 | 0 | OPENSSL_free(s->s3.tmp.peer_cert_sigalgs); |
1489 | 0 | s->s3.tmp.peer_cert_sigalgs = NULL; |
1490 | 0 | s->s3.tmp.peer_cert_sigalgslen = 0; |
1491 | |
|
1492 | 0 | return 1; |
1493 | 0 | } |
1494 | | |
1495 | | #ifndef OPENSSL_NO_SRP |
1496 | | static int init_srp(SSL_CONNECTION *s, unsigned int context) |
1497 | 0 | { |
1498 | 0 | OPENSSL_free(s->srp_ctx.login); |
1499 | 0 | s->srp_ctx.login = NULL; |
1500 | |
|
1501 | 0 | return 1; |
1502 | 0 | } |
1503 | | #endif |
1504 | | |
1505 | | static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context) |
1506 | 0 | { |
1507 | 0 | OPENSSL_free(s->ext.peer_ecpointformats); |
1508 | 0 | s->ext.peer_ecpointformats = NULL; |
1509 | 0 | s->ext.peer_ecpointformats_len = 0; |
1510 | |
|
1511 | 0 | return 1; |
1512 | 0 | } |
1513 | | |
1514 | | static int init_etm(SSL_CONNECTION *s, unsigned int context) |
1515 | 0 | { |
1516 | 0 | s->ext.use_etm = 0; |
1517 | |
|
1518 | 0 | return 1; |
1519 | 0 | } |
1520 | | |
1521 | | static int init_ems(SSL_CONNECTION *s, unsigned int context) |
1522 | 0 | { |
1523 | 0 | if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) { |
1524 | 0 | s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; |
1525 | 0 | s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS; |
1526 | 0 | } |
1527 | |
|
1528 | 0 | return 1; |
1529 | 0 | } |
1530 | | |
1531 | | static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent) |
1532 | 0 | { |
1533 | | /* |
1534 | | * Check extended master secret extension is not dropped on |
1535 | | * renegotiation. |
1536 | | */ |
1537 | 0 | if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) |
1538 | 0 | && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) { |
1539 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS); |
1540 | 0 | return 0; |
1541 | 0 | } |
1542 | 0 | if (!s->server && s->hit) { |
1543 | | /* |
1544 | | * Check extended master secret extension is consistent with |
1545 | | * original session. |
1546 | | */ |
1547 | 0 | if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) != !(s->session->flags & SSL_SESS_FLAG_EXTMS)) { |
1548 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS); |
1549 | 0 | return 0; |
1550 | 0 | } |
1551 | 0 | } |
1552 | | |
1553 | 0 | return 1; |
1554 | 0 | } |
1555 | | |
1556 | | static int init_certificate_authorities(SSL_CONNECTION *s, unsigned int context) |
1557 | 0 | { |
1558 | 0 | sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free); |
1559 | 0 | s->s3.tmp.peer_ca_names = NULL; |
1560 | 0 | return 1; |
1561 | 0 | } |
1562 | | |
1563 | | static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s, |
1564 | | WPACKET *pkt, |
1565 | | unsigned int context, |
1566 | | X509 *x, |
1567 | | size_t chainidx) |
1568 | 0 | { |
1569 | 0 | const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s); |
1570 | |
|
1571 | 0 | if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0) |
1572 | 0 | return EXT_RETURN_NOT_SENT; |
1573 | | |
1574 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities) |
1575 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
1576 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1577 | 0 | return EXT_RETURN_FAIL; |
1578 | 0 | } |
1579 | | |
1580 | 0 | if (!construct_ca_names(s, ca_sk, pkt)) { |
1581 | | /* SSLfatal() already called */ |
1582 | 0 | return EXT_RETURN_FAIL; |
1583 | 0 | } |
1584 | | |
1585 | 0 | if (!WPACKET_close(pkt)) { |
1586 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1587 | 0 | return EXT_RETURN_FAIL; |
1588 | 0 | } |
1589 | | |
1590 | 0 | return EXT_RETURN_SENT; |
1591 | 0 | } |
1592 | | |
1593 | | static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt, |
1594 | | unsigned int context, X509 *x, |
1595 | | size_t chainidx) |
1596 | 0 | { |
1597 | 0 | if (!parse_ca_names(s, pkt)) |
1598 | 0 | return 0; |
1599 | 0 | if (PACKET_remaining(pkt) != 0) { |
1600 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1601 | 0 | return 0; |
1602 | 0 | } |
1603 | 0 | return 1; |
1604 | 0 | } |
1605 | | |
1606 | | #ifndef OPENSSL_NO_SRTP |
1607 | | static int init_srtp(SSL_CONNECTION *s, unsigned int context) |
1608 | 0 | { |
1609 | 0 | if (s->server) |
1610 | 0 | s->srtp_profile = NULL; |
1611 | |
|
1612 | 0 | return 1; |
1613 | 0 | } |
1614 | | #endif |
1615 | | |
1616 | | static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent) |
1617 | 0 | { |
1618 | 0 | if (!sent && SSL_CONNECTION_IS_TLS13(s) && !s->hit) { |
1619 | 0 | SSLfatal(s, TLS13_AD_MISSING_EXTENSION, |
1620 | 0 | SSL_R_MISSING_SIGALGS_EXTENSION); |
1621 | 0 | return 0; |
1622 | 0 | } |
1623 | | |
1624 | 0 | return 1; |
1625 | 0 | } |
1626 | | |
1627 | | static int final_supported_versions(SSL_CONNECTION *s, unsigned int context, |
1628 | | int sent) |
1629 | 0 | { |
1630 | 0 | if (!sent && context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) { |
1631 | 0 | SSLfatal(s, TLS13_AD_MISSING_EXTENSION, |
1632 | 0 | SSL_R_MISSING_SUPPORTED_VERSIONS_EXTENSION); |
1633 | 0 | return 0; |
1634 | 0 | } |
1635 | | |
1636 | 0 | return 1; |
1637 | 0 | } |
1638 | | |
1639 | | static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent) |
1640 | 0 | { |
1641 | 0 | #if !defined(OPENSSL_NO_TLS1_3) |
1642 | 0 | if (!SSL_CONNECTION_IS_TLS13(s)) |
1643 | 0 | return 1; |
1644 | | |
1645 | | /* Nothing to do for key_share in an HRR */ |
1646 | 0 | if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) |
1647 | 0 | return 1; |
1648 | | |
1649 | | /* |
1650 | | * If |
1651 | | * we are a client |
1652 | | * AND |
1653 | | * we have no key_share |
1654 | | * AND |
1655 | | * (we are not resuming |
1656 | | * OR the kex_mode doesn't allow non key_share resumes) |
1657 | | * THEN |
1658 | | * fail; |
1659 | | */ |
1660 | 0 | if (!s->server |
1661 | 0 | && !sent) { |
1662 | 0 | if ((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) { |
1663 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_SUITABLE_KEY_SHARE); |
1664 | 0 | return 0; |
1665 | 0 | } |
1666 | 0 | if (!s->hit) { |
1667 | 0 | SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE); |
1668 | 0 | return 0; |
1669 | 0 | } |
1670 | 0 | } |
1671 | | /* |
1672 | | * IF |
1673 | | * we are a server |
1674 | | * THEN |
1675 | | * IF |
1676 | | * we have a suitable key_share |
1677 | | * THEN |
1678 | | * IF |
1679 | | * we are stateless AND we have no cookie |
1680 | | * THEN |
1681 | | * send a HelloRetryRequest |
1682 | | * ELSE |
1683 | | * IF |
1684 | | * we didn't already send a HelloRetryRequest |
1685 | | * AND |
1686 | | * the client sent a key_share extension |
1687 | | * AND |
1688 | | * (we are not resuming |
1689 | | * OR the kex_mode allows key_share resumes) |
1690 | | * AND |
1691 | | * a shared group exists |
1692 | | * THEN |
1693 | | * send a HelloRetryRequest |
1694 | | * ELSE IF |
1695 | | * we are not resuming |
1696 | | * OR |
1697 | | * the kex_mode doesn't allow non key_share resumes |
1698 | | * THEN |
1699 | | * fail |
1700 | | * ELSE IF |
1701 | | * we are stateless AND we have no cookie |
1702 | | * THEN |
1703 | | * send a HelloRetryRequest |
1704 | | */ |
1705 | 0 | if (s->server) { |
1706 | 0 | if (s->s3.peer_tmp != NULL) { |
1707 | | /* We have a suitable key_share */ |
1708 | 0 | if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0 |
1709 | 0 | && !s->ext.cookieok) { |
1710 | 0 | if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) { |
1711 | | /* |
1712 | | * If we are stateless then we wouldn't know about any |
1713 | | * previously sent HRR - so how can this be anything other |
1714 | | * than 0? |
1715 | | */ |
1716 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1717 | 0 | return 0; |
1718 | 0 | } |
1719 | 0 | s->hello_retry_request = SSL_HRR_PENDING; |
1720 | 0 | return 1; |
1721 | 0 | } |
1722 | 0 | } else { |
1723 | | /* No suitable key_share */ |
1724 | 0 | if (s->hello_retry_request == SSL_HRR_NONE && sent |
1725 | 0 | && (!s->hit |
1726 | 0 | || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) != 0)) { |
1727 | | |
1728 | | /* Did we detect group overlap in tls_parse_ctos_key_share ? */ |
1729 | 0 | if (s->s3.group_id_candidate != 0) { |
1730 | | /* A shared group exists so send a HelloRetryRequest */ |
1731 | 0 | s->s3.group_id = s->s3.group_id_candidate; |
1732 | 0 | s->hello_retry_request = SSL_HRR_PENDING; |
1733 | 0 | return 1; |
1734 | 0 | } |
1735 | 0 | } |
1736 | 0 | if (!s->hit |
1737 | 0 | || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) { |
1738 | | /* Nothing left we can do - just fail */ |
1739 | 0 | SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE : SSL_AD_MISSING_EXTENSION, |
1740 | 0 | SSL_R_NO_SUITABLE_KEY_SHARE); |
1741 | 0 | return 0; |
1742 | 0 | } |
1743 | | |
1744 | 0 | if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0 |
1745 | 0 | && !s->ext.cookieok) { |
1746 | 0 | if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) { |
1747 | | /* |
1748 | | * If we are stateless then we wouldn't know about any |
1749 | | * previously sent HRR - so how can this be anything other |
1750 | | * than 0? |
1751 | | */ |
1752 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1753 | 0 | return 0; |
1754 | 0 | } |
1755 | 0 | s->hello_retry_request = SSL_HRR_PENDING; |
1756 | 0 | return 1; |
1757 | 0 | } |
1758 | 0 | } |
1759 | | |
1760 | | /* |
1761 | | * We have a key_share so don't send any more HelloRetryRequest |
1762 | | * messages |
1763 | | */ |
1764 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) |
1765 | 0 | s->hello_retry_request = SSL_HRR_COMPLETE; |
1766 | 0 | } else { |
1767 | | /* |
1768 | | * For a client side resumption with no key_share we need to generate |
1769 | | * the handshake secret (otherwise this is done during key_share |
1770 | | * processing). |
1771 | | */ |
1772 | 0 | if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) { |
1773 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1774 | 0 | return 0; |
1775 | 0 | } |
1776 | 0 | } |
1777 | 0 | #endif /* !defined(OPENSSL_NO_TLS1_3) */ |
1778 | 0 | return 1; |
1779 | 0 | } |
1780 | | |
1781 | | static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context) |
1782 | 0 | { |
1783 | 0 | s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE; |
1784 | 0 | return 1; |
1785 | 0 | } |
1786 | | |
1787 | | int tls_psk_do_binder(SSL_CONNECTION *s, const EVP_MD *md, |
1788 | | const unsigned char *msgstart, |
1789 | | size_t binderoffset, const unsigned char *binderin, |
1790 | | unsigned char *binderout, SSL_SESSION *sess, int sign, |
1791 | | int external) |
1792 | 0 | { |
1793 | 0 | EVP_PKEY *mackey = NULL; |
1794 | 0 | EVP_MD_CTX *mctx = NULL; |
1795 | 0 | unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE]; |
1796 | 0 | unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE]; |
1797 | 0 | unsigned char *early_secret; |
1798 | | /* ASCII: "res binder", in hex for EBCDIC compatibility */ |
1799 | 0 | static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72"; |
1800 | | /* ASCII: "ext binder", in hex for EBCDIC compatibility */ |
1801 | 0 | static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72"; |
1802 | 0 | const unsigned char *label; |
1803 | 0 | size_t bindersize, labelsize, hashsize; |
1804 | 0 | int hashsizei = EVP_MD_get_size(md); |
1805 | 0 | int ret = -1; |
1806 | 0 | int usepskfored = 0; |
1807 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1808 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
1809 | | |
1810 | | /* Ensure cast to size_t is safe */ |
1811 | 0 | if (!ossl_assert(hashsizei > 0)) { |
1812 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1813 | 0 | goto err; |
1814 | 0 | } |
1815 | 0 | hashsize = (size_t)hashsizei; |
1816 | |
|
1817 | 0 | if (external |
1818 | 0 | && s->early_data_state == SSL_EARLY_DATA_CONNECTING |
1819 | 0 | && s->session->ext.max_early_data == 0 |
1820 | 0 | && sess->ext.max_early_data > 0) |
1821 | 0 | usepskfored = 1; |
1822 | |
|
1823 | 0 | if (external) { |
1824 | 0 | label = external_label; |
1825 | 0 | labelsize = sizeof(external_label) - 1; |
1826 | 0 | } else { |
1827 | 0 | label = resumption_label; |
1828 | 0 | labelsize = sizeof(resumption_label) - 1; |
1829 | 0 | } |
1830 | | |
1831 | | /* |
1832 | | * Generate the early_secret. On the server side we've selected a PSK to |
1833 | | * resume with (internal or external) so we always do this. On the client |
1834 | | * side we do this for a non-external (i.e. resumption) PSK or external PSK |
1835 | | * that will be used for early_data so that it is in place for sending early |
1836 | | * data. For client side external PSK not being used for early_data we |
1837 | | * generate it but store it away for later use. |
1838 | | */ |
1839 | 0 | if (s->server || !external || usepskfored) |
1840 | 0 | early_secret = (unsigned char *)s->early_secret; |
1841 | 0 | else |
1842 | 0 | early_secret = (unsigned char *)sess->early_secret; |
1843 | |
|
1844 | 0 | if (!tls13_generate_secret(s, md, NULL, sess->master_key, |
1845 | 0 | sess->master_key_length, early_secret)) { |
1846 | | /* SSLfatal() already called */ |
1847 | 0 | goto err; |
1848 | 0 | } |
1849 | | |
1850 | | /* |
1851 | | * Create the handshake hash for the binder key...the messages so far are |
1852 | | * empty! |
1853 | | */ |
1854 | 0 | mctx = EVP_MD_CTX_new(); |
1855 | 0 | if (mctx == NULL |
1856 | 0 | || EVP_DigestInit_ex(mctx, md, NULL) <= 0 |
1857 | 0 | || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { |
1858 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1859 | 0 | goto err; |
1860 | 0 | } |
1861 | | |
1862 | | /* Generate the binder key */ |
1863 | 0 | if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash, |
1864 | 0 | hashsize, binderkey, hashsize, 1)) { |
1865 | | /* SSLfatal() already called */ |
1866 | 0 | goto err; |
1867 | 0 | } |
1868 | | |
1869 | | /* Generate the finished key */ |
1870 | 0 | if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) { |
1871 | | /* SSLfatal() already called */ |
1872 | 0 | goto err; |
1873 | 0 | } |
1874 | | |
1875 | 0 | if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) { |
1876 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1877 | 0 | goto err; |
1878 | 0 | } |
1879 | | |
1880 | | /* |
1881 | | * Get a hash of the ClientHello up to the start of the binders. If we are |
1882 | | * following a HelloRetryRequest then this includes the hash of the first |
1883 | | * ClientHello and the HelloRetryRequest itself. |
1884 | | */ |
1885 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
1886 | 0 | size_t hdatalen; |
1887 | 0 | long hdatalen_l; |
1888 | 0 | void *hdata; |
1889 | |
|
1890 | 0 | #ifndef OPENSSL_NO_ECH |
1891 | | /* handle the hashing as per ECH needs (on client) */ |
1892 | 0 | if (s->ext.ech.attempted == 1 && s->ext.ech.ch_depth == 1) { |
1893 | 0 | if (ossl_ech_intbuf_fetch(s, (unsigned char **)&hdata, &hdatalen) != 1) { |
1894 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1895 | 0 | goto err; |
1896 | 0 | } |
1897 | 0 | } else { |
1898 | 0 | #endif |
1899 | 0 | hdatalen = hdatalen_l = BIO_get_mem_data(s->s3.handshake_buffer, &hdata); |
1900 | 0 | if (hdatalen_l <= 0) { |
1901 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH); |
1902 | 0 | goto err; |
1903 | 0 | } |
1904 | 0 | #ifndef OPENSSL_NO_ECH |
1905 | 0 | } |
1906 | 0 | #endif |
1907 | | |
1908 | | /* |
1909 | | * For servers the handshake buffer data will include the second |
1910 | | * ClientHello - which we don't want - so we need to take that bit off. |
1911 | | */ |
1912 | 0 | if (s->server) { |
1913 | 0 | PACKET hashprefix, msg; |
1914 | | |
1915 | | /* Find how many bytes are left after the first two messages */ |
1916 | 0 | if (!PACKET_buf_init(&hashprefix, hdata, hdatalen) |
1917 | 0 | || !PACKET_forward(&hashprefix, 1) |
1918 | 0 | || !PACKET_get_length_prefixed_3(&hashprefix, &msg) |
1919 | 0 | || !PACKET_forward(&hashprefix, 1) |
1920 | 0 | || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) { |
1921 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1922 | 0 | goto err; |
1923 | 0 | } |
1924 | 0 | hdatalen -= PACKET_remaining(&hashprefix); |
1925 | 0 | } |
1926 | | |
1927 | 0 | if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) { |
1928 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1929 | 0 | goto err; |
1930 | 0 | } |
1931 | 0 | } |
1932 | | |
1933 | 0 | if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0 |
1934 | 0 | || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { |
1935 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1936 | 0 | goto err; |
1937 | 0 | } |
1938 | | |
1939 | 0 | mackey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC", |
1940 | 0 | sctx->propq, finishedkey, |
1941 | 0 | hashsize); |
1942 | 0 | if (mackey == NULL) { |
1943 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1944 | 0 | goto err; |
1945 | 0 | } |
1946 | | |
1947 | 0 | if (!sign) |
1948 | 0 | binderout = tmpbinder; |
1949 | |
|
1950 | 0 | if (sctx->propq != NULL) |
1951 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, |
1952 | 0 | (char *)sctx->propq, 0); |
1953 | 0 | bindersize = hashsize; |
1954 | 0 | if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), sctx->libctx, |
1955 | 0 | sctx->propq, mackey, params) |
1956 | 0 | <= 0 |
1957 | 0 | || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0 |
1958 | 0 | || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0 |
1959 | 0 | || bindersize != hashsize) { |
1960 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1961 | 0 | goto err; |
1962 | 0 | } |
1963 | | |
1964 | 0 | if (sign) { |
1965 | 0 | ret = 1; |
1966 | 0 | } else { |
1967 | | /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */ |
1968 | 0 | ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0); |
1969 | 0 | if (!ret) |
1970 | 0 | SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BINDER_DOES_NOT_VERIFY); |
1971 | 0 | } |
1972 | |
|
1973 | 0 | err: |
1974 | 0 | OPENSSL_cleanse(binderkey, sizeof(binderkey)); |
1975 | 0 | OPENSSL_cleanse(finishedkey, sizeof(finishedkey)); |
1976 | 0 | EVP_PKEY_free(mackey); |
1977 | 0 | EVP_MD_CTX_free(mctx); |
1978 | 0 | return ret; |
1979 | 0 | } |
1980 | | |
1981 | | static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent) |
1982 | 0 | { |
1983 | 0 | if (!sent) |
1984 | 0 | return 1; |
1985 | | |
1986 | 0 | if (!s->server) { |
1987 | 0 | if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS |
1988 | 0 | && sent |
1989 | 0 | && !s->ext.early_data_ok) { |
1990 | | /* |
1991 | | * If we get here then the server accepted our early_data but we |
1992 | | * later realised that it shouldn't have done (e.g. inconsistent |
1993 | | * ALPN) |
1994 | | */ |
1995 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA); |
1996 | 0 | return 0; |
1997 | 0 | } |
1998 | | |
1999 | 0 | return 1; |
2000 | 0 | } |
2001 | | |
2002 | 0 | if (s->max_early_data == 0 |
2003 | 0 | || !s->hit |
2004 | 0 | || s->early_data_state != SSL_EARLY_DATA_ACCEPTING |
2005 | 0 | || !s->ext.early_data_ok |
2006 | 0 | || s->hello_retry_request != SSL_HRR_NONE |
2007 | 0 | || (s->allow_early_data_cb != NULL |
2008 | 0 | && !s->allow_early_data_cb(SSL_CONNECTION_GET_USER_SSL(s), |
2009 | 0 | s->allow_early_data_cb_data))) { |
2010 | 0 | s->ext.early_data = SSL_EARLY_DATA_REJECTED; |
2011 | 0 | } else { |
2012 | 0 | s->ext.early_data = SSL_EARLY_DATA_ACCEPTED; |
2013 | |
|
2014 | 0 | if (!tls13_change_cipher_state(s, |
2015 | 0 | SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) { |
2016 | | /* SSLfatal() already called */ |
2017 | 0 | return 0; |
2018 | 0 | } |
2019 | 0 | } |
2020 | | |
2021 | 0 | return 1; |
2022 | 0 | } |
2023 | | |
2024 | | static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context, |
2025 | | int sent) |
2026 | 0 | { |
2027 | 0 | if (s->session == NULL) |
2028 | 0 | return 1; |
2029 | | |
2030 | | /* MaxFragmentLength defaults to disabled */ |
2031 | 0 | if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED) |
2032 | 0 | s->session->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_DISABLED; |
2033 | |
|
2034 | 0 | if (USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) { |
2035 | 0 | s->rlayer.rrlmethod->set_max_frag_len(s->rlayer.rrl, |
2036 | 0 | GET_MAX_FRAGMENT_LENGTH(s->session)); |
2037 | 0 | s->rlayer.wrlmethod->set_max_frag_len(s->rlayer.wrl, |
2038 | 0 | ssl_get_max_send_fragment(s)); |
2039 | 0 | } |
2040 | |
|
2041 | 0 | return 1; |
2042 | 0 | } |
2043 | | |
2044 | | static int init_post_handshake_auth(SSL_CONNECTION *s, |
2045 | | ossl_unused unsigned int context) |
2046 | 0 | { |
2047 | 0 | s->post_handshake_auth = SSL_PHA_NONE; |
2048 | |
|
2049 | 0 | return 1; |
2050 | 0 | } |
2051 | | |
2052 | | /* |
2053 | | * If clients offer "pre_shared_key" without a "psk_key_exchange_modes" |
2054 | | * extension, servers MUST abort the handshake. |
2055 | | */ |
2056 | | static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent) |
2057 | 0 | { |
2058 | 0 | if (s->server && sent && s->clienthello != NULL |
2059 | 0 | && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) { |
2060 | 0 | SSLfatal(s, TLS13_AD_MISSING_EXTENSION, |
2061 | 0 | SSL_R_MISSING_PSK_KEX_MODES_EXTENSION); |
2062 | 0 | return 0; |
2063 | 0 | } |
2064 | | |
2065 | 0 | return 1; |
2066 | 0 | } |
2067 | | |
2068 | | static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context) |
2069 | 0 | { |
2070 | 0 | memset(sc->ext.compress_certificate_from_peer, 0, |
2071 | 0 | sizeof(sc->ext.compress_certificate_from_peer)); |
2072 | 0 | return 1; |
2073 | 0 | } |
2074 | | |
2075 | | /* The order these are put into the packet imply a preference order: [brotli, zlib, zstd] */ |
2076 | | static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt, |
2077 | | unsigned int context, |
2078 | | X509 *x, size_t chainidx) |
2079 | 0 | { |
2080 | | #ifndef OPENSSL_NO_COMP_ALG |
2081 | | int i; |
2082 | | |
2083 | | if (!ossl_comp_has_alg(0)) |
2084 | | return EXT_RETURN_NOT_SENT; |
2085 | | |
2086 | | /* Server: Don't attempt to compress a non-X509 (i.e. an RPK) */ |
2087 | | if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509) { |
2088 | | sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none; |
2089 | | return EXT_RETURN_NOT_SENT; |
2090 | | } |
2091 | | |
2092 | | /* Client: If we sent a client cert-type extension, don't indicate compression */ |
2093 | | if (!sc->server && sc->ext.client_cert_type_ctos) { |
2094 | | sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none; |
2095 | | return EXT_RETURN_NOT_SENT; |
2096 | | } |
2097 | | |
2098 | | /* Do not indicate we support receiving compressed certificates */ |
2099 | | if ((sc->options & SSL_OP_NO_RX_CERTIFICATE_COMPRESSION) != 0) |
2100 | | return EXT_RETURN_NOT_SENT; |
2101 | | |
2102 | | if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none) |
2103 | | return EXT_RETURN_NOT_SENT; |
2104 | | #ifndef OPENSSL_NO_ECH |
2105 | | ECH_SAME_EXT(sc, context, pkt); |
2106 | | #endif |
2107 | | |
2108 | | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_compress_certificate) |
2109 | | || !WPACKET_start_sub_packet_u16(pkt) |
2110 | | || !WPACKET_start_sub_packet_u8(pkt)) |
2111 | | goto err; |
2112 | | |
2113 | | for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) { |
2114 | | if (!WPACKET_put_bytes_u16(pkt, sc->cert_comp_prefs[i])) |
2115 | | goto err; |
2116 | | } |
2117 | | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) |
2118 | | goto err; |
2119 | | |
2120 | | sc->ext.compress_certificate_sent = 1; |
2121 | | return EXT_RETURN_SENT; |
2122 | | err: |
2123 | | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2124 | | return EXT_RETURN_FAIL; |
2125 | | #else |
2126 | 0 | return EXT_RETURN_NOT_SENT; |
2127 | 0 | #endif |
2128 | 0 | } |
2129 | | |
2130 | | #ifndef OPENSSL_NO_COMP_ALG |
2131 | | static int tls_comp_in_pref(SSL_CONNECTION *sc, int alg) |
2132 | | { |
2133 | | int i; |
2134 | | |
2135 | | /* ossl_comp_has_alg() considers 0 as "any" */ |
2136 | | if (alg == 0) |
2137 | | return 0; |
2138 | | /* Make sure algorithm is enabled */ |
2139 | | if (!ossl_comp_has_alg(alg)) |
2140 | | return 0; |
2141 | | /* If no preferences are set, it's ok */ |
2142 | | if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none) |
2143 | | return 1; |
2144 | | /* Find the algorithm */ |
2145 | | for (i = 0; i < TLSEXT_comp_cert_limit; i++) |
2146 | | if (sc->cert_comp_prefs[i] == alg) |
2147 | | return 1; |
2148 | | return 0; |
2149 | | } |
2150 | | #endif |
2151 | | |
2152 | | int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, unsigned int context, |
2153 | | X509 *x, size_t chainidx) |
2154 | 0 | { |
2155 | | #ifndef OPENSSL_NO_COMP_ALG |
2156 | | PACKET supported_comp_algs; |
2157 | | unsigned int comp; |
2158 | | int already_set[TLSEXT_comp_cert_limit]; |
2159 | | int j = 0; |
2160 | | |
2161 | | /* If no algorithms are available, ignore the extension */ |
2162 | | if (!ossl_comp_has_alg(0)) |
2163 | | return 1; |
2164 | | |
2165 | | /* Don't attempt to compress a non-X509 (i.e. an RPK) */ |
2166 | | if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509) |
2167 | | return 1; |
2168 | | if (!sc->server && sc->ext.client_cert_type != TLSEXT_cert_type_x509) |
2169 | | return 1; |
2170 | | |
2171 | | /* Ignore the extension and don't send compressed certificates */ |
2172 | | if ((sc->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) |
2173 | | return 1; |
2174 | | |
2175 | | if (!PACKET_as_length_prefixed_1(pkt, &supported_comp_algs) |
2176 | | || PACKET_remaining(&supported_comp_algs) == 0) { |
2177 | | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2178 | | return 0; |
2179 | | } |
2180 | | |
2181 | | memset(already_set, 0, sizeof(already_set)); |
2182 | | /* |
2183 | | * The preference array has real values, so take a look at each |
2184 | | * value coming in, and make sure it's in our preference list |
2185 | | * The array is 0 (i.e. "none") terminated |
2186 | | * The preference list only contains supported algorithms |
2187 | | */ |
2188 | | while (PACKET_get_net_2(&supported_comp_algs, &comp)) { |
2189 | | if (tls_comp_in_pref(sc, comp) && !already_set[comp]) { |
2190 | | sc->ext.compress_certificate_from_peer[j++] = comp; |
2191 | | already_set[comp] = 1; |
2192 | | } |
2193 | | } |
2194 | | if (PACKET_remaining(&supported_comp_algs) != 0) { |
2195 | | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2196 | | return 0; |
2197 | | } |
2198 | | #endif |
2199 | 0 | return 1; |
2200 | 0 | } |
2201 | | |
2202 | | static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context) |
2203 | 0 | { |
2204 | | /* Only reset when parsing client hello */ |
2205 | 0 | if (sc->server) { |
2206 | 0 | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2207 | 0 | sc->ext.server_cert_type = TLSEXT_cert_type_x509; |
2208 | 0 | } |
2209 | 0 | return 1; |
2210 | 0 | } |
2211 | | |
2212 | | static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context) |
2213 | 0 | { |
2214 | | /* Only reset when parsing client hello */ |
2215 | 0 | if (sc->server) { |
2216 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2217 | 0 | sc->ext.client_cert_type = TLSEXT_cert_type_x509; |
2218 | 0 | } |
2219 | 0 | return 1; |
2220 | 0 | } |