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