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