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