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