/src/openssl41/ssl/statem/extensions_clnt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/ocsp.h> |
11 | | #include <openssl/rand.h> |
12 | | #include "../ssl_local.h" |
13 | | #include "internal/cryptlib.h" |
14 | | #include "internal/ssl_unwrap.h" |
15 | | #include "internal/tlsgroups.h" |
16 | | #include "statem_local.h" |
17 | | #ifndef OPENSSL_NO_ECH |
18 | | #include "internal/ech_helpers.h" |
19 | | #endif |
20 | | |
21 | | /* Used in the negotiate_dhe function */ |
22 | | typedef enum { |
23 | | ffdhe_check, |
24 | | ecdhe_check, |
25 | | ptfmt_check |
26 | | } dhe_check_t; |
27 | | |
28 | | EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt, |
29 | | unsigned int context, X509 *x, |
30 | | size_t chainidx) |
31 | 42.9k | { |
32 | 42.9k | if (!s->renegotiate) { |
33 | 42.5k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION |
34 | 42.5k | : TLS1_3_VERSION; |
35 | | |
36 | | /* If not renegotiating, send an empty RI extension to indicate support */ |
37 | 42.5k | if ((s->min_proto_version != 0 |
38 | 16.0k | && ssl_version_cmp(s, s->min_proto_version, version1_3) >= 0) |
39 | 26.4k | || (!SSL_CONNECTION_IS_DTLS(s) |
40 | 11.7k | && ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL) |
41 | 27.8k | && s->min_proto_version <= TLS1_VERSION)) { |
42 | | /* |
43 | | * For TLS <= 1.0 SCSV is used instead, and for TLS 1.3 this |
44 | | * extension isn't used at all. |
45 | | */ |
46 | 27.8k | return EXT_RETURN_NOT_SENT; |
47 | 27.8k | } |
48 | | |
49 | 14.6k | #ifndef OPENSSL_NO_ECH |
50 | 14.6k | ECH_SAME_EXT(s, context, pkt) |
51 | 14.6k | #endif |
52 | | |
53 | 14.6k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
54 | 14.6k | || !WPACKET_start_sub_packet_u16(pkt) |
55 | 14.6k | || !WPACKET_put_bytes_u8(pkt, 0) |
56 | 14.6k | || !WPACKET_close(pkt)) { |
57 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
58 | 0 | return EXT_RETURN_FAIL; |
59 | 0 | } |
60 | | |
61 | 14.6k | return EXT_RETURN_SENT; |
62 | 14.6k | } |
63 | | |
64 | 365 | #ifndef OPENSSL_NO_ECH |
65 | 365 | ECH_SAME_EXT(s, context, pkt) |
66 | 365 | #endif |
67 | | |
68 | | /* Add a complete RI extension if renegotiating */ |
69 | 365 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
70 | 365 | || !WPACKET_start_sub_packet_u16(pkt) |
71 | 365 | || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished, |
72 | 365 | s->s3.previous_client_finished_len) |
73 | 365 | || !WPACKET_close(pkt)) { |
74 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
75 | 0 | return EXT_RETURN_FAIL; |
76 | 0 | } |
77 | | |
78 | 365 | return EXT_RETURN_SENT; |
79 | 365 | } |
80 | | |
81 | | EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt, |
82 | | unsigned int context, X509 *x, |
83 | | size_t chainidx) |
84 | 60.8k | { |
85 | 60.8k | char *chosen = s->ext.hostname; |
86 | 60.8k | #ifndef OPENSSL_NO_ECH |
87 | 60.8k | OSSL_HPKE_SUITE suite; |
88 | 60.8k | OSSL_ECHSTORE_ENTRY *ee = NULL; |
89 | | |
90 | 60.8k | if (s->ext.ech.es != NULL) { |
91 | 0 | if (ossl_ech_pick_matching_cfg(s, &ee, &suite) != 1) { |
92 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
93 | 0 | return EXT_RETURN_NOT_SENT; |
94 | 0 | } |
95 | | /* Don't send outer SNI if external API says so */ |
96 | 0 | if (s->ext.ech.ch_depth == 0 && s->ext.ech.no_outer == 1) |
97 | 0 | return EXT_RETURN_NOT_SENT; |
98 | 0 | if (s->ext.ech.ch_depth == 1) /* inner */ |
99 | 0 | chosen = s->ext.hostname; |
100 | 0 | if (s->ext.ech.ch_depth == 0) { /* outer */ |
101 | 0 | if (s->ext.ech.outer_hostname != NULL) /* prefer API */ |
102 | 0 | chosen = s->ext.ech.outer_hostname; |
103 | 0 | else /* use name from ECHConfig */ |
104 | 0 | chosen = ee->public_name; |
105 | 0 | } |
106 | 0 | } |
107 | 60.8k | #endif |
108 | 60.8k | if (chosen == NULL) |
109 | 0 | return EXT_RETURN_NOT_SENT; |
110 | | /* Add TLS extension servername to the Client Hello message */ |
111 | 60.8k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) |
112 | | /* Sub-packet for server_name extension */ |
113 | 60.8k | || !WPACKET_start_sub_packet_u16(pkt) |
114 | | /* Sub-packet for servername list (always 1 hostname)*/ |
115 | 60.8k | || !WPACKET_start_sub_packet_u16(pkt) |
116 | 60.8k | || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name) |
117 | 60.8k | || !WPACKET_sub_memcpy_u16(pkt, chosen, strlen(chosen)) |
118 | 60.8k | || !WPACKET_close(pkt) |
119 | 60.8k | || !WPACKET_close(pkt)) { |
120 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
121 | 0 | return EXT_RETURN_FAIL; |
122 | 0 | } |
123 | 60.8k | return EXT_RETURN_SENT; |
124 | 60.8k | } |
125 | | |
126 | | /* Push a Max Fragment Len extension into ClientHello */ |
127 | | EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt, |
128 | | unsigned int context, X509 *x, |
129 | | size_t chainidx) |
130 | 60.8k | { |
131 | 60.8k | if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED) |
132 | 60.8k | return EXT_RETURN_NOT_SENT; |
133 | 0 | #ifndef OPENSSL_NO_ECH |
134 | 0 | ECH_SAME_EXT(s, context, pkt) |
135 | 0 | #endif |
136 | | |
137 | | /* Add Max Fragment Length extension if client enabled it. */ |
138 | | /*- |
139 | | * 4 bytes for this extension type and extension length |
140 | | * 1 byte for the Max Fragment Length code value. |
141 | | */ |
142 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length) |
143 | | /* Sub-packet for Max Fragment Length extension (1 byte) */ |
144 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
145 | 0 | || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode) |
146 | 0 | || !WPACKET_close(pkt)) { |
147 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
148 | 0 | return EXT_RETURN_FAIL; |
149 | 0 | } |
150 | | |
151 | 0 | return EXT_RETURN_SENT; |
152 | 0 | } |
153 | | |
154 | | #ifndef OPENSSL_NO_SRP |
155 | | EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt, |
156 | | unsigned int context, |
157 | | X509 *x, size_t chainidx) |
158 | 60.8k | { |
159 | | /* Add SRP username if there is one */ |
160 | 60.8k | if (s->srp_ctx.login == NULL) |
161 | 60.8k | return EXT_RETURN_NOT_SENT; |
162 | 0 | #ifndef OPENSSL_NO_ECH |
163 | 0 | ECH_SAME_EXT(s, context, pkt) |
164 | 0 | #endif |
165 | | |
166 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp) |
167 | | /* Sub-packet for SRP extension */ |
168 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
169 | 0 | || !WPACKET_start_sub_packet_u8(pkt) |
170 | | /* login must not be zero...internal error if so */ |
171 | 0 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
172 | 0 | || !WPACKET_memcpy(pkt, s->srp_ctx.login, |
173 | 0 | strlen(s->srp_ctx.login)) |
174 | 0 | || !WPACKET_close(pkt) |
175 | 0 | || !WPACKET_close(pkt)) { |
176 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
177 | 0 | return EXT_RETURN_FAIL; |
178 | 0 | } |
179 | | |
180 | 0 | return EXT_RETURN_SENT; |
181 | 0 | } |
182 | | #endif |
183 | | |
184 | | /* |
185 | | * With (D)TLS < 1.3 the only negotiated supported key exchange groups are |
186 | | * FFDHE (RFC7919) and ECDHE/ECX (RFC8422 + legacy). With (D)TLS 1.3, we add |
187 | | * KEMs, and the supported groups are no longer cipher-dependent. |
188 | | * |
189 | | * This function serves two purposes: |
190 | | * |
191 | | * - To determine whether to send the supported point formats extension. |
192 | | * This is no longer applicable with (D)TLS >= 1.3. |
193 | | * - To determine whether to send the supported groups extension. |
194 | | * |
195 | | * In the former case, we only care about whether both ECC ciphers and EC/ECX |
196 | | * supported groups are configured, and the (D)TLS min version is at most 1.2. |
197 | | * |
198 | | * In the latter case, we also admit DHE ciphers with FFDHE groups, or any TLS |
199 | | * 1.3 cipher, since the extension is effectively mandatory for (D)TLS 1.3, |
200 | | * with the sole exception of psk-ke resumption, provided the client is sure |
201 | | * that the server will not want elect a full handshake. The check type then |
202 | | * indicates whether ECDHE or FFDHE negotiation should be performed. |
203 | | * |
204 | | * It returns 1 if negotiation is supported, 0 if it's not and -1 on error. |
205 | | */ |
206 | | static int negotiate_dhe(SSL_CONNECTION *s, dhe_check_t check_type, |
207 | | int min_version, int max_version) |
208 | 128k | { |
209 | 128k | int i, end, ret = 0; |
210 | 128k | STACK_OF(SSL_CIPHER) *cipher_stack = NULL; |
211 | 128k | const uint16_t *pgroups = NULL; |
212 | 128k | size_t num_groups, j; |
213 | 128k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
214 | 128k | int dtls = SSL_CONNECTION_IS_DTLS(s); |
215 | | |
216 | | /* See if we support any EC or FFDHE ciphersuites */ |
217 | 128k | cipher_stack = SSL_get1_supported_ciphers(ssl); |
218 | 128k | if (cipher_stack == NULL) |
219 | 0 | return -1; |
220 | 128k | end = sk_SSL_CIPHER_num(cipher_stack); |
221 | 256k | for (i = 0; i < end; i++) { |
222 | 240k | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
223 | 240k | unsigned long alg_k = c->algorithm_mkey; |
224 | 240k | unsigned long alg_a = c->algorithm_auth; |
225 | | |
226 | 240k | int is_ffdhe_ciphersuite = (alg_k & (SSL_kDHE | SSL_kDHEPSK)); |
227 | 240k | int is_ec_ciphersuite = ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) |
228 | 210k | || (alg_a & SSL_aECDSA)); |
229 | 240k | int is_tls13 = (dtls ? DTLS_VERSION_GT(c->min_dtls, DTLS1_2_VERSION) |
230 | 240k | : (c->min_tls > TLS1_2_VERSION)); |
231 | | |
232 | 240k | if ((check_type == ffdhe_check && (is_ffdhe_ciphersuite || is_tls13)) |
233 | 197k | || (check_type == ecdhe_check && (is_ec_ciphersuite || is_tls13)) |
234 | 154k | || (check_type == ptfmt_check && is_ec_ciphersuite)) { |
235 | 112k | ret = 1; |
236 | 112k | break; |
237 | 112k | } |
238 | 240k | } |
239 | 128k | sk_SSL_CIPHER_free(cipher_stack); |
240 | 128k | if (ret == 0) |
241 | 16.0k | return 0; |
242 | | |
243 | | /* Check we have at least one EC or FFDHE supported group */ |
244 | 112k | tls1_get_supported_groups(s, &pgroups, &num_groups); |
245 | 710k | for (j = 0; j < num_groups; j++) { |
246 | 710k | uint16_t ctmp = pgroups[j]; |
247 | 710k | const TLS_GROUP_INFO *ginfo = NULL; |
248 | | |
249 | 710k | if (!tls_valid_group(s, ctmp, min_version, max_version, NULL, &ginfo)) |
250 | 12.2k | continue; |
251 | | |
252 | 697k | if (check_type == ffdhe_check && is_ffdhe_group(ginfo->group_id) |
253 | 42.7k | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
254 | 42.7k | return 1; |
255 | | |
256 | 655k | if (check_type != ffdhe_check && is_ecdhe_group(ginfo->group_id) |
257 | 69.4k | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
258 | 69.4k | return 1; |
259 | 655k | } |
260 | 414 | return 0; |
261 | 112k | } |
262 | | |
263 | | EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt, |
264 | | unsigned int context, X509 *x, |
265 | | size_t chainidx) |
266 | 42.9k | { |
267 | 42.9k | const unsigned char *pformats; |
268 | 42.9k | size_t num_formats; |
269 | 42.9k | int reason, min_version, max_version, dhe_result; |
270 | | |
271 | 42.9k | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
272 | 42.9k | if (reason != 0) { |
273 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
274 | 0 | return EXT_RETURN_FAIL; |
275 | 0 | } |
276 | 42.9k | dhe_result = negotiate_dhe(s, ptfmt_check, min_version, max_version); |
277 | 42.9k | if (dhe_result == 0) |
278 | 16.2k | return EXT_RETURN_NOT_SENT; |
279 | 26.6k | if (dhe_result < 0) { |
280 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
281 | 0 | return EXT_RETURN_FAIL; |
282 | 0 | } |
283 | | |
284 | 26.6k | tls1_get_formatlist(s, &pformats, &num_formats); |
285 | 26.6k | if (num_formats == 0) |
286 | 0 | return EXT_RETURN_NOT_SENT; |
287 | 26.6k | #ifndef OPENSSL_NO_ECH |
288 | 26.6k | ECH_SAME_EXT(s, context, pkt) |
289 | 26.6k | #endif |
290 | | |
291 | | /* Add TLS extension ECPointFormats to the ClientHello message */ |
292 | 26.6k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
293 | | /* Sub-packet for formats extension */ |
294 | 26.6k | || !WPACKET_start_sub_packet_u16(pkt) |
295 | 26.6k | || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats) |
296 | 26.6k | || !WPACKET_close(pkt)) { |
297 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
298 | 0 | return EXT_RETURN_FAIL; |
299 | 0 | } |
300 | | |
301 | 26.6k | return EXT_RETURN_SENT; |
302 | 26.6k | } |
303 | | |
304 | | EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt, |
305 | | unsigned int context, X509 *x, |
306 | | size_t chainidx) |
307 | 42.9k | { |
308 | 42.9k | const uint16_t *pgroups = NULL; |
309 | 42.9k | size_t num_groups = 0, i, tls13added = 0, added = 0; |
310 | 42.9k | int min_version, max_version, reason; |
311 | 42.9k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
312 | 42.9k | int dtls = SSL_CONNECTION_IS_DTLS(s); |
313 | 42.9k | int use_ecdhe, use_ffdhe; |
314 | | |
315 | 42.9k | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
316 | 42.9k | if (reason != 0) { |
317 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
318 | 0 | return EXT_RETURN_FAIL; |
319 | 0 | } |
320 | | |
321 | | /* |
322 | | * If we don't support suitable groups, don't send the extension |
323 | | */ |
324 | 42.9k | use_ecdhe = negotiate_dhe(s, ecdhe_check, min_version, max_version); |
325 | 42.9k | use_ffdhe = negotiate_dhe(s, ffdhe_check, min_version, max_version); |
326 | 42.9k | if (use_ecdhe < 0 || use_ffdhe < 0) { |
327 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
328 | 0 | return EXT_RETURN_FAIL; |
329 | 0 | } |
330 | 42.9k | if (use_ecdhe == 0 && use_ffdhe == 0 |
331 | 138 | && (dtls ? DTLS_VERSION_LE(max_version, DTLS1_2_VERSION) |
332 | 138 | : (max_version <= TLS1_2_VERSION))) |
333 | 138 | return EXT_RETURN_NOT_SENT; |
334 | 42.7k | #ifndef OPENSSL_NO_ECH |
335 | 42.7k | ECH_SAME_EXT(s, context, pkt) |
336 | 42.7k | #endif |
337 | | |
338 | | /* |
339 | | * Add TLS extension supported_groups to the ClientHello message |
340 | | */ |
341 | 42.7k | tls1_get_supported_groups(s, &pgroups, &num_groups); |
342 | | |
343 | 42.7k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) |
344 | | /* Sub-packet for supported_groups extension */ |
345 | 42.7k | || !WPACKET_start_sub_packet_u16(pkt) |
346 | 42.7k | || !WPACKET_start_sub_packet_u16(pkt) |
347 | 42.7k | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) { |
348 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
349 | 0 | return EXT_RETURN_FAIL; |
350 | 0 | } |
351 | | /* RFC 8701: prepend a GREASE group value */ |
352 | 42.7k | if ((s->options & SSL_OP_GREASE) && !s->server) { |
353 | 0 | if (!WPACKET_put_bytes_u16(pkt, |
354 | 0 | ossl_grease_value(s, OSSL_GREASE_GROUP))) { |
355 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
356 | 0 | return EXT_RETURN_FAIL; |
357 | 0 | } |
358 | 0 | } |
359 | | /* Copy group ID if supported */ |
360 | 513k | for (i = 0; i < num_groups; i++) { |
361 | 470k | const TLS_GROUP_INFO *ginfo = NULL; |
362 | 470k | uint16_t ctmp = pgroups[i]; |
363 | 470k | int okfortls13; |
364 | | |
365 | 470k | if (!tls_valid_group(s, ctmp, min_version, max_version, &okfortls13, |
366 | 470k | &ginfo) |
367 | 467k | || (!use_ecdhe && is_ecdhe_group(ginfo->group_id)) |
368 | 467k | || (!use_ffdhe && is_ffdhe_group(ginfo->group_id)) |
369 | | /* Note: SSL_SECOP_CURVE_SUPPORTED covers all key exchange groups */ |
370 | 467k | || !tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
371 | 3.07k | continue; |
372 | | |
373 | 467k | if (!WPACKET_put_bytes_u16(pkt, ctmp)) { |
374 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
375 | 0 | return EXT_RETURN_FAIL; |
376 | 0 | } |
377 | 467k | if (okfortls13 && max_version == version1_3) |
378 | 462k | tls13added++; |
379 | 467k | added++; |
380 | 467k | } |
381 | 42.7k | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
382 | 0 | if (added == 0) |
383 | 0 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS, |
384 | 0 | "No groups enabled for max supported SSL/TLS version"); |
385 | 0 | else |
386 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
387 | 0 | return EXT_RETURN_FAIL; |
388 | 0 | } |
389 | | |
390 | 42.7k | if (tls13added == 0 && max_version == version1_3) { |
391 | 0 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS, |
392 | 0 | "No groups enabled for max supported SSL/TLS version"); |
393 | 0 | return EXT_RETURN_FAIL; |
394 | 0 | } |
395 | | |
396 | 42.7k | return EXT_RETURN_SENT; |
397 | 42.7k | } |
398 | | |
399 | | EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt, |
400 | | unsigned int context, X509 *x, |
401 | | size_t chainidx) |
402 | 42.9k | { |
403 | 42.9k | size_t ticklen; |
404 | 42.9k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
405 | | |
406 | 42.9k | if (!tls_use_ticket(s)) |
407 | 0 | return EXT_RETURN_NOT_SENT; |
408 | 42.9k | #ifndef OPENSSL_NO_ECH |
409 | 42.9k | ECH_SAME_EXT(s, context, pkt) |
410 | 42.9k | #endif |
411 | | |
412 | 42.9k | if (!s->new_session && s->session != NULL |
413 | 42.9k | && s->session->ext.tick != NULL |
414 | 41 | && s->session->ssl_version != version1_3) { |
415 | 41 | ticklen = s->session->ext.ticklen; |
416 | 42.8k | } else if (s->session && s->ext.session_ticket != NULL |
417 | 0 | && s->ext.session_ticket->data != NULL) { |
418 | 0 | ticklen = s->ext.session_ticket->length; |
419 | 0 | s->session->ext.tick = OPENSSL_malloc(ticklen); |
420 | 0 | if (s->session->ext.tick == NULL) { |
421 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
422 | 0 | return EXT_RETURN_FAIL; |
423 | 0 | } |
424 | 0 | memcpy(s->session->ext.tick, |
425 | 0 | s->ext.session_ticket->data, ticklen); |
426 | 0 | s->session->ext.ticklen = ticklen; |
427 | 42.8k | } else { |
428 | 42.8k | ticklen = 0; |
429 | 42.8k | } |
430 | | |
431 | 42.9k | if (ticklen == 0 && s->ext.session_ticket != NULL && s->ext.session_ticket->data == NULL) |
432 | 0 | return EXT_RETURN_NOT_SENT; |
433 | | |
434 | 42.9k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
435 | 42.9k | || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) { |
436 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
437 | 0 | return EXT_RETURN_FAIL; |
438 | 0 | } |
439 | | |
440 | 42.9k | return EXT_RETURN_SENT; |
441 | 42.9k | } |
442 | | |
443 | | EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt, |
444 | | unsigned int context, X509 *x, |
445 | | size_t chainidx) |
446 | 42.9k | { |
447 | 42.9k | size_t salglen; |
448 | 42.9k | const uint16_t *salg; |
449 | | |
450 | | /* |
451 | | * This used both in the initial hello and as part of renegotiation, |
452 | | * in the latter case, the client version may be already set and may |
453 | | * be lower than that initially offered in `client_version`. |
454 | | */ |
455 | 42.9k | if (!SSL_CONNECTION_IS_DTLS(s)) { |
456 | 28.2k | if (s->client_version < TLS1_2_VERSION |
457 | 28.2k | || (s->ssl.method->version != TLS_ANY_VERSION |
458 | 365 | && s->version < TLS1_2_VERSION)) |
459 | 331 | return EXT_RETURN_NOT_SENT; |
460 | 28.2k | } else { |
461 | 14.6k | if (DTLS_VERSION_LT(s->client_version, DTLS1_2_VERSION) |
462 | 14.3k | || (s->ssl.method->version != DTLS_ANY_VERSION |
463 | 188 | && DTLS_VERSION_LT(s->version, DTLS1_2_VERSION))) |
464 | 353 | return EXT_RETURN_NOT_SENT; |
465 | 14.6k | } |
466 | | |
467 | 42.2k | #ifndef OPENSSL_NO_ECH |
468 | 42.2k | ECH_SAME_EXT(s, context, pkt) |
469 | 42.2k | #endif |
470 | | |
471 | 42.2k | salglen = tls12_get_psigalgs(s, 1, &salg); |
472 | 42.2k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) |
473 | | /* Sub-packet for sig-algs extension */ |
474 | 42.2k | || !WPACKET_start_sub_packet_u16(pkt) |
475 | | /* Sub-packet for the actual list */ |
476 | 42.2k | || !WPACKET_start_sub_packet_u16(pkt) |
477 | 42.2k | || !tls12_copy_sigalgs(s, pkt, salg, salglen)) { |
478 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
479 | 0 | return EXT_RETURN_FAIL; |
480 | 0 | } |
481 | | /* RFC 8701: append a GREASE signature algorithm value */ |
482 | 42.2k | if ((s->options & SSL_OP_GREASE) && !s->server) { |
483 | 0 | if (!WPACKET_put_bytes_u16(pkt, |
484 | 0 | ossl_grease_value(s, OSSL_GREASE_SIGALG))) { |
485 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
486 | 0 | return EXT_RETURN_FAIL; |
487 | 0 | } |
488 | 0 | } |
489 | 42.2k | if (!WPACKET_close(pkt) |
490 | 42.2k | || !WPACKET_close(pkt)) { |
491 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
492 | 0 | return EXT_RETURN_FAIL; |
493 | 0 | } |
494 | | |
495 | 42.2k | return EXT_RETURN_SENT; |
496 | 42.2k | } |
497 | | |
498 | | #ifndef OPENSSL_NO_OCSP |
499 | | EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt, |
500 | | unsigned int context, X509 *x, |
501 | | size_t chainidx) |
502 | 60.8k | { |
503 | 60.8k | int i; |
504 | | |
505 | | /* This extension isn't defined for client Certificates */ |
506 | 60.8k | if (x != NULL) |
507 | 0 | return EXT_RETURN_NOT_SENT; |
508 | | |
509 | 60.8k | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) |
510 | 60.8k | return EXT_RETURN_NOT_SENT; |
511 | 0 | #ifndef OPENSSL_NO_ECH |
512 | 0 | ECH_SAME_EXT(s, context, pkt) |
513 | 0 | #endif |
514 | | |
515 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
516 | | /* Sub-packet for status request extension */ |
517 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
518 | 0 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp) |
519 | | /* Sub-packet for the ids */ |
520 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
521 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
522 | 0 | return EXT_RETURN_FAIL; |
523 | 0 | } |
524 | 0 | for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) { |
525 | 0 | unsigned char *idbytes; |
526 | 0 | OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i); |
527 | 0 | int idlen = i2d_OCSP_RESPID(id, NULL); |
528 | |
|
529 | 0 | if (idlen <= 0 |
530 | | /* Sub-packet for an individual id */ |
531 | 0 | || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) |
532 | 0 | || i2d_OCSP_RESPID(id, &idbytes) != idlen) { |
533 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
534 | 0 | return EXT_RETURN_FAIL; |
535 | 0 | } |
536 | 0 | } |
537 | 0 | if (!WPACKET_close(pkt) |
538 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
539 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
540 | 0 | return EXT_RETURN_FAIL; |
541 | 0 | } |
542 | 0 | if (s->ext.ocsp.exts) { |
543 | 0 | unsigned char *extbytes; |
544 | 0 | int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL); |
545 | |
|
546 | 0 | if (extlen < 0) { |
547 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
548 | 0 | return EXT_RETURN_FAIL; |
549 | 0 | } |
550 | 0 | if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes) |
551 | 0 | || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes) |
552 | 0 | != extlen) { |
553 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
554 | 0 | return EXT_RETURN_FAIL; |
555 | 0 | } |
556 | 0 | } |
557 | 0 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
558 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
559 | 0 | return EXT_RETURN_FAIL; |
560 | 0 | } |
561 | | |
562 | 0 | return EXT_RETURN_SENT; |
563 | 0 | } |
564 | | #endif |
565 | | |
566 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
567 | | EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt, |
568 | | unsigned int context, |
569 | | X509 *x, size_t chainidx) |
570 | 60.8k | { |
571 | 60.8k | if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL |
572 | 0 | || !SSL_IS_FIRST_HANDSHAKE(s)) |
573 | 60.8k | return EXT_RETURN_NOT_SENT; |
574 | 0 | #ifndef OPENSSL_NO_ECH |
575 | 0 | ECH_SAME_EXT(s, context, pkt) |
576 | 0 | #endif |
577 | | |
578 | | /* |
579 | | * The client advertises an empty extension to indicate its support |
580 | | * for Next Protocol Negotiation |
581 | | */ |
582 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
583 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
584 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
585 | 0 | return EXT_RETURN_FAIL; |
586 | 0 | } |
587 | | |
588 | 0 | return EXT_RETURN_SENT; |
589 | 0 | } |
590 | | #endif |
591 | | |
592 | | EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt, |
593 | | unsigned int context, |
594 | | X509 *x, size_t chainidx) |
595 | 60.8k | { |
596 | 60.8k | unsigned char *aval = s->ext.alpn; |
597 | 60.8k | size_t alen = s->ext.alpn_len; |
598 | | |
599 | 60.8k | s->s3.alpn_sent = 0; |
600 | 60.8k | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
601 | 597 | return EXT_RETURN_NOT_SENT; |
602 | 60.2k | #ifndef OPENSSL_NO_ECH |
603 | | /* |
604 | | * If we have different alpn and alpn_outer values, then we set |
605 | | * the appropriate one for inner and outer. |
606 | | * If no alpn is set (for inner or outer), we don't send any. |
607 | | * If only an inner is set then we send the same in both. |
608 | | * Logic above is on the basis that alpn's aren't that sensitive, |
609 | | * usually, so special action is needed to do better. |
610 | | * We also don't support a way to send alpn only in the inner. |
611 | | * If you don't want the inner value in the outer, you have to |
612 | | * pick what to send in the outer and send that. |
613 | | */ |
614 | 60.2k | if (s->ext.ech.ch_depth == 1 && s->ext.alpn == NULL) /* inner */ |
615 | 0 | return EXT_RETURN_NOT_SENT; |
616 | 60.2k | if (s->ext.ech.ch_depth == 0 && s->ext.alpn == NULL |
617 | 35.6k | && s->ext.ech.alpn_outer == NULL) /* outer */ |
618 | 35.6k | return EXT_RETURN_NOT_SENT; |
619 | 24.5k | if (s->ext.ech.ch_depth == 0 && s->ext.ech.alpn_outer != NULL) { |
620 | 0 | aval = s->ext.ech.alpn_outer; |
621 | 0 | alen = s->ext.ech.alpn_outer_len; |
622 | 0 | } |
623 | 24.5k | #endif |
624 | 24.5k | if (aval == NULL) |
625 | 0 | return EXT_RETURN_NOT_SENT; |
626 | 24.5k | if (!WPACKET_put_bytes_u16(pkt, |
627 | 24.5k | TLSEXT_TYPE_application_layer_protocol_negotiation) |
628 | | /* Sub-packet ALPN extension */ |
629 | 24.5k | || !WPACKET_start_sub_packet_u16(pkt) |
630 | 24.5k | || !WPACKET_sub_memcpy_u16(pkt, aval, alen) |
631 | 24.5k | || !WPACKET_close(pkt)) { |
632 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
633 | 0 | return EXT_RETURN_FAIL; |
634 | 0 | } |
635 | 24.5k | s->s3.alpn_sent = 1; |
636 | 24.5k | return EXT_RETURN_SENT; |
637 | 24.5k | } |
638 | | |
639 | | #ifndef OPENSSL_NO_SRTP |
640 | | EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt, |
641 | | unsigned int context, X509 *x, |
642 | | size_t chainidx) |
643 | 60.8k | { |
644 | 60.8k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
645 | 60.8k | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl); |
646 | 60.8k | int i, end; |
647 | | |
648 | 60.8k | if (clnt == NULL) |
649 | 60.8k | return EXT_RETURN_NOT_SENT; |
650 | 0 | #ifndef OPENSSL_NO_ECH |
651 | 0 | ECH_SAME_EXT(s, context, pkt) |
652 | 0 | #endif |
653 | | |
654 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
655 | | /* Sub-packet for SRTP extension */ |
656 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
657 | | /* Sub-packet for the protection profile list */ |
658 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
659 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
660 | 0 | return EXT_RETURN_FAIL; |
661 | 0 | } |
662 | | |
663 | 0 | end = sk_SRTP_PROTECTION_PROFILE_num(clnt); |
664 | 0 | for (i = 0; i < end; i++) { |
665 | 0 | const SRTP_PROTECTION_PROFILE *prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); |
666 | |
|
667 | 0 | if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { |
668 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
669 | 0 | return EXT_RETURN_FAIL; |
670 | 0 | } |
671 | 0 | } |
672 | 0 | if (!WPACKET_close(pkt) |
673 | | /* Add an empty use_mki value */ |
674 | 0 | || !WPACKET_put_bytes_u8(pkt, 0) |
675 | 0 | || !WPACKET_close(pkt)) { |
676 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
677 | 0 | return EXT_RETURN_FAIL; |
678 | 0 | } |
679 | | |
680 | 0 | return EXT_RETURN_SENT; |
681 | 0 | } |
682 | | #endif |
683 | | |
684 | | EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt, |
685 | | unsigned int context, |
686 | | X509 *x, size_t chainidx) |
687 | 60.8k | { |
688 | 60.8k | if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) |
689 | 0 | return EXT_RETURN_NOT_SENT; |
690 | 60.8k | #ifndef OPENSSL_NO_ECH |
691 | 60.8k | ECH_SAME_EXT(s, context, pkt) |
692 | 60.8k | #endif |
693 | | |
694 | 60.8k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) |
695 | 60.8k | || !WPACKET_put_bytes_u16(pkt, 0)) { |
696 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
697 | 0 | return EXT_RETURN_FAIL; |
698 | 0 | } |
699 | | |
700 | 60.8k | return EXT_RETURN_SENT; |
701 | 60.8k | } |
702 | | |
703 | | #ifndef OPENSSL_NO_CT |
704 | | EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt, |
705 | | unsigned int context, |
706 | | X509 *x, size_t chainidx) |
707 | 60.8k | { |
708 | 60.8k | if (s->ct_validation_callback == NULL) |
709 | 60.8k | return EXT_RETURN_NOT_SENT; |
710 | | |
711 | | /* Not defined for client Certificates */ |
712 | 0 | if (x != NULL) |
713 | 0 | return EXT_RETURN_NOT_SENT; |
714 | 0 | #ifndef OPENSSL_NO_ECH |
715 | 0 | ECH_SAME_EXT(s, context, pkt) |
716 | 0 | #endif |
717 | | |
718 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp) |
719 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
720 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
721 | 0 | return EXT_RETURN_FAIL; |
722 | 0 | } |
723 | | |
724 | 0 | return EXT_RETURN_SENT; |
725 | 0 | } |
726 | | #endif |
727 | | |
728 | | EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt, |
729 | | unsigned int context, |
730 | | X509 *x, size_t chainidx) |
731 | 60.8k | { |
732 | 60.8k | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
733 | 0 | return EXT_RETURN_NOT_SENT; |
734 | 60.8k | #ifndef OPENSSL_NO_ECH |
735 | 60.8k | ECH_SAME_EXT(s, context, pkt) |
736 | 60.8k | #endif |
737 | | |
738 | 60.8k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
739 | 60.8k | || !WPACKET_put_bytes_u16(pkt, 0)) { |
740 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
741 | 0 | return EXT_RETURN_FAIL; |
742 | 0 | } |
743 | | |
744 | 60.8k | return EXT_RETURN_SENT; |
745 | 60.8k | } |
746 | | |
747 | | EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt, |
748 | | unsigned int context, X509 *x, |
749 | | size_t chainidx) |
750 | 42.9k | { |
751 | 42.9k | int currv, min_version, max_version, reason; |
752 | 42.9k | const int isdtls = SSL_CONNECTION_IS_DTLS(s); |
753 | 42.9k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
754 | | |
755 | 42.9k | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
756 | 42.9k | if (reason != 0) { |
757 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
758 | 0 | return EXT_RETURN_FAIL; |
759 | 0 | } |
760 | | |
761 | | /* |
762 | | * Don't include this if we can't negotiate (D)TLSv1.3. |
763 | | */ |
764 | 42.9k | if (ssl_version_cmp(s, max_version, version1_3) < 0) |
765 | 906 | return EXT_RETURN_NOT_SENT; |
766 | 42.0k | #ifndef OPENSSL_NO_ECH |
767 | 42.0k | ECH_SAME_EXT(s, context, pkt) |
768 | 42.0k | #endif |
769 | | |
770 | 42.0k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) |
771 | 42.0k | || !WPACKET_start_sub_packet_u16(pkt) |
772 | 42.0k | || !WPACKET_start_sub_packet_u8(pkt)) { |
773 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
774 | 0 | return EXT_RETURN_FAIL; |
775 | 0 | } |
776 | | |
777 | | /* RFC 8701: prepend a GREASE version value */ |
778 | 42.0k | if ((s->options & SSL_OP_GREASE) && !s->server) { |
779 | 0 | if (!WPACKET_put_bytes_u16(pkt, |
780 | 0 | ossl_grease_value(s, OSSL_GREASE_VERSION))) { |
781 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
782 | 0 | return EXT_RETURN_FAIL; |
783 | 0 | } |
784 | 0 | } |
785 | 175k | for (currv = max_version; ssl_version_cmp(s, currv, min_version) >= 0; |
786 | 133k | isdtls ? currv++ : currv--) { |
787 | 133k | if (!WPACKET_put_bytes_u16(pkt, currv)) { |
788 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
789 | 0 | return EXT_RETURN_FAIL; |
790 | 0 | } |
791 | 133k | } |
792 | 42.0k | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
793 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
794 | 0 | return EXT_RETURN_FAIL; |
795 | 0 | } |
796 | | |
797 | 42.0k | return EXT_RETURN_SENT; |
798 | 42.0k | } |
799 | | |
800 | | /* |
801 | | * Construct a psk_kex_modes extension. |
802 | | */ |
803 | | EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt, |
804 | | unsigned int context, X509 *x, |
805 | | size_t chainidx) |
806 | 56.4k | { |
807 | 56.4k | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
808 | 56.4k | int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX; |
809 | | |
810 | 56.4k | #ifndef OPENSSL_NO_ECH |
811 | 56.4k | ECH_SAME_EXT(s, context, pkt) |
812 | 56.4k | #endif |
813 | | |
814 | 56.4k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes) |
815 | 56.4k | || !WPACKET_start_sub_packet_u16(pkt) |
816 | 56.4k | || !WPACKET_start_sub_packet_u8(pkt) |
817 | 56.4k | || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE) |
818 | 56.4k | || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE)) |
819 | 56.4k | || !WPACKET_close(pkt) |
820 | 56.4k | || !WPACKET_close(pkt)) { |
821 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
822 | 0 | return EXT_RETURN_FAIL; |
823 | 0 | } |
824 | | |
825 | 56.4k | s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE; |
826 | 56.4k | if (nodhe) |
827 | 0 | s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE; |
828 | 56.4k | #endif |
829 | | |
830 | 56.4k | return EXT_RETURN_SENT; |
831 | 56.4k | } |
832 | | |
833 | | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
834 | | static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int group_id, size_t loop_num) |
835 | 83.9k | { |
836 | 83.9k | unsigned char *encoded_pubkey = NULL; |
837 | 83.9k | EVP_PKEY *key_share_key = NULL; |
838 | 83.9k | size_t encodedlen; |
839 | | |
840 | 83.9k | if (loop_num < s->s3.tmp.num_ks_pkey) { |
841 | 2.95k | if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING |
842 | 2.95k | || s->d1->hello_verify_request == SSL_HVR_RECEIVED) |
843 | 2.95k | || !ossl_assert(s->s3.tmp.ks_pkey[loop_num] != NULL)) { |
844 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
845 | 0 | return 0; |
846 | 0 | } |
847 | | /* |
848 | | * Could happen if we got a HRR that wasn't requesting a new key_share |
849 | | * or if we got a HelloVerifyRequest |
850 | | */ |
851 | 2.95k | key_share_key = s->s3.tmp.ks_pkey[loop_num]; |
852 | 80.9k | } else { |
853 | 80.9k | key_share_key = ssl_generate_pkey_group(s, group_id); |
854 | 80.9k | if (key_share_key == NULL) { |
855 | | /* SSLfatal() already called */ |
856 | 0 | return 0; |
857 | 0 | } |
858 | 80.9k | } |
859 | | |
860 | | /* Encode the public key. */ |
861 | 83.9k | encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key, |
862 | 83.9k | &encoded_pubkey); |
863 | 83.9k | if (encodedlen == 0) { |
864 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
865 | 0 | goto err; |
866 | 0 | } |
867 | | |
868 | | /* Create KeyShareEntry */ |
869 | 83.9k | if (!WPACKET_put_bytes_u16(pkt, group_id) |
870 | 83.9k | || !WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encodedlen)) { |
871 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
872 | 0 | goto err; |
873 | 0 | } |
874 | | |
875 | | /* For backward compatibility, we use the first valid group to add a key share */ |
876 | 83.9k | if (loop_num == 0) { |
877 | 42.0k | s->s3.tmp.pkey = key_share_key; |
878 | 42.0k | s->s3.group_id = group_id; |
879 | 42.0k | } |
880 | | /* We ensure in t1_lib.c that the loop number does not exceed OPENSSL_CLIENT_MAX_KEY_SHARES */ |
881 | 83.9k | s->s3.tmp.ks_pkey[loop_num] = key_share_key; |
882 | 83.9k | s->s3.tmp.ks_group_id[loop_num] = group_id; |
883 | 83.9k | if (loop_num >= s->s3.tmp.num_ks_pkey) |
884 | 80.9k | s->s3.tmp.num_ks_pkey++; |
885 | | |
886 | 83.9k | OPENSSL_free(encoded_pubkey); |
887 | 83.9k | return 1; |
888 | 0 | err: |
889 | 0 | if (key_share_key != s->s3.tmp.ks_pkey[loop_num]) |
890 | 0 | EVP_PKEY_free(key_share_key); |
891 | 0 | OPENSSL_free(encoded_pubkey); |
892 | 0 | return 0; |
893 | 83.9k | } |
894 | | #endif |
895 | | |
896 | | EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt, |
897 | | unsigned int context, X509 *x, |
898 | | size_t chainidx) |
899 | 42.0k | { |
900 | 42.0k | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
901 | 42.0k | size_t i, num_groups = 0; |
902 | 42.0k | const uint16_t *pgroups = NULL; |
903 | 42.0k | uint16_t group_id = 0; |
904 | 42.0k | int add_only_one = 0; |
905 | 42.0k | size_t valid_keyshare = 0; |
906 | | |
907 | 42.0k | #ifndef OPENSSL_NO_ECH |
908 | 42.0k | ECH_SAME_EXT(s, context, pkt) |
909 | 42.0k | #endif |
910 | | |
911 | | /* key_share extension */ |
912 | 42.0k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) |
913 | | /* Extension data sub-packet */ |
914 | 42.0k | || !WPACKET_start_sub_packet_u16(pkt) |
915 | | /* KeyShare list sub-packet */ |
916 | 42.0k | || !WPACKET_start_sub_packet_u16(pkt)) { |
917 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
918 | 0 | return EXT_RETURN_FAIL; |
919 | 0 | } |
920 | | |
921 | | /* |
922 | | * RFC 8701: prepend a GREASE key share entry (1 byte of 0x00). After an |
923 | | * HRR requesting a new key share, RFC 9846 requires the requested entry |
924 | | * to be the only one in the second ClientHello. |
925 | | */ |
926 | 42.0k | if ((s->options & SSL_OP_GREASE) && !s->server |
927 | 0 | && !(s->hello_retry_request == SSL_HRR_PENDING |
928 | 0 | && s->s3.group_id != 0 && s->s3.tmp.pkey == NULL)) { |
929 | 0 | uint16_t grease_group = ossl_grease_value(s, OSSL_GREASE_GROUP); |
930 | |
|
931 | 0 | if (!WPACKET_put_bytes_u16(pkt, grease_group) |
932 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
933 | 0 | || !WPACKET_put_bytes_u8(pkt, 0) |
934 | 0 | || !WPACKET_close(pkt)) { |
935 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
936 | 0 | return EXT_RETURN_FAIL; |
937 | 0 | } |
938 | 0 | } |
939 | | |
940 | 42.0k | tls1_get_requested_keyshare_groups(s, &pgroups, &num_groups); |
941 | 42.0k | if (num_groups == 1 && pgroups[0] == 0) { /* Indication that no * prefix was used */ |
942 | 0 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
943 | 0 | add_only_one = 1; |
944 | 0 | } |
945 | | |
946 | | /* If neither the default nor the keyshares have any entry --> fatal */ |
947 | 42.0k | if (num_groups == 0) { |
948 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
949 | 0 | return EXT_RETURN_FAIL; |
950 | 0 | } |
951 | | |
952 | | /* Add key shares */ |
953 | | |
954 | 42.0k | if (s->s3.group_id != 0 && s->s3.tmp.pkey == NULL) { |
955 | | /* new, single key share */ |
956 | 102 | group_id = s->s3.group_id; |
957 | 102 | s->s3.tmp.num_ks_pkey = 0; |
958 | 102 | if (!add_key_share(s, pkt, group_id, 0)) { |
959 | | /* SSLfatal() already called */ |
960 | 0 | return EXT_RETURN_FAIL; |
961 | 0 | } |
962 | 102 | valid_keyshare++; |
963 | 41.9k | } else { |
964 | 41.9k | if (s->ext.supportedgroups == NULL) /* use default */ |
965 | 0 | add_only_one = 1; |
966 | | |
967 | 125k | for (i = 0; i < num_groups; i++) { |
968 | 83.8k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION |
969 | 83.8k | : TLS1_3_VERSION; |
970 | | |
971 | 83.8k | if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED)) |
972 | 0 | continue; |
973 | | |
974 | 83.8k | if (!tls_valid_group(s, pgroups[i], version1_3, version1_3, |
975 | 83.8k | NULL, NULL)) |
976 | 0 | continue; |
977 | | |
978 | 83.8k | group_id = pgroups[i]; |
979 | | |
980 | 83.8k | if (group_id == 0) { |
981 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
982 | 0 | return EXT_RETURN_FAIL; |
983 | 0 | } |
984 | 83.8k | if (!add_key_share(s, pkt, group_id, valid_keyshare)) { |
985 | | /* SSLfatal() already called */ |
986 | 0 | return EXT_RETURN_FAIL; |
987 | 0 | } |
988 | 83.8k | valid_keyshare++; |
989 | 83.8k | if (add_only_one) |
990 | 0 | break; |
991 | 83.8k | } |
992 | 41.9k | } |
993 | | |
994 | 42.0k | if (valid_keyshare == 0) { |
995 | | /* No key shares were allowed */ |
996 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
997 | 0 | return EXT_RETURN_FAIL; |
998 | 0 | } |
999 | | |
1000 | 42.0k | #ifndef OPENSSL_NO_ECH |
1001 | | /* stash inner key shares */ |
1002 | 42.0k | if (s->ext.ech.ch_depth == 1 && ossl_ech_stash_keyshares(s) != 1) { |
1003 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1004 | 0 | return EXT_RETURN_FAIL; |
1005 | 0 | } |
1006 | 42.0k | #endif |
1007 | | |
1008 | 42.0k | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
1009 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1010 | 0 | return EXT_RETURN_FAIL; |
1011 | 0 | } |
1012 | 42.0k | return EXT_RETURN_SENT; |
1013 | | #else |
1014 | | return EXT_RETURN_NOT_SENT; |
1015 | | #endif |
1016 | 42.0k | } |
1017 | | |
1018 | | EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt, |
1019 | | unsigned int context, |
1020 | | X509 *x, size_t chainidx) |
1021 | 56.4k | { |
1022 | 56.4k | EXT_RETURN ret = EXT_RETURN_FAIL; |
1023 | | |
1024 | | /* Should only be set if we've had an HRR */ |
1025 | 56.4k | if (s->ext.tls13_cookie_len == 0) |
1026 | 56.1k | return EXT_RETURN_NOT_SENT; |
1027 | 298 | #ifndef OPENSSL_NO_ECH |
1028 | 298 | ECH_SAME_EXT(s, context, pkt) |
1029 | 298 | #endif |
1030 | | |
1031 | 298 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie) |
1032 | | /* Extension data sub-packet */ |
1033 | 298 | || !WPACKET_start_sub_packet_u16(pkt) |
1034 | 298 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie, |
1035 | 298 | s->ext.tls13_cookie_len) |
1036 | 298 | || !WPACKET_close(pkt)) { |
1037 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1038 | 0 | goto end; |
1039 | 0 | } |
1040 | | |
1041 | 298 | ret = EXT_RETURN_SENT; |
1042 | 298 | end: |
1043 | 298 | OPENSSL_free(s->ext.tls13_cookie); |
1044 | 298 | s->ext.tls13_cookie = NULL; |
1045 | 298 | s->ext.tls13_cookie_len = 0; |
1046 | | |
1047 | 298 | return ret; |
1048 | 298 | } |
1049 | | |
1050 | | static int tls13_check_tick_lifetime_hint(SSL_CONNECTION *s) |
1051 | 0 | { |
1052 | 0 | OSSL_TIME t; |
1053 | 0 | uint32_t agesec; |
1054 | 0 | unsigned long lifetime; |
1055 | |
|
1056 | 0 | if (s->ext.tick_age_checked) |
1057 | 0 | return s->ext.tick_age_ok; |
1058 | 0 | s->ext.tick_age_ok = 1; |
1059 | | |
1060 | | /* |
1061 | | * Technically the C standard just says time() returns a time_t and says |
1062 | | * nothing about the encoding of that type. In practice most |
1063 | | * implementations follow POSIX which holds it as an integral type in |
1064 | | * seconds since epoch. We've already made the assumption that we can do |
1065 | | * this in multiple places in the code, so portability shouldn't be an |
1066 | | * issue. |
1067 | | */ |
1068 | 0 | t = ossl_time_subtract(ossl_time_now(), s->session->time); |
1069 | 0 | agesec = (uint32_t)ossl_time2seconds(t); |
1070 | | |
1071 | | /* |
1072 | | * We calculate the age in seconds but the server may work in ms. Due to |
1073 | | * rounding errors we could overestimate the age by up to 1s. It is |
1074 | | * better to underestimate it. Otherwise, if the RTT is very short, when |
1075 | | * the server calculates the age reported by the client it could be |
1076 | | * bigger than the age calculated on the server - which should never |
1077 | | * happen. |
1078 | | */ |
1079 | 0 | if (agesec > 0) |
1080 | 0 | agesec--; |
1081 | | |
1082 | | /* |
1083 | | * Calculate age in ms. We're just doing it to nearest second. Should be |
1084 | | * good enough. |
1085 | | */ |
1086 | 0 | s->ext.tick_age_ms = agesec * (uint32_t)1000; |
1087 | | |
1088 | | /* |
1089 | | * RFC 9846 4.7.1: Servers MUST NOT use any value greater than 604800 |
1090 | | * seconds (7 days). The value of zero indicates that the ticket should be |
1091 | | * discarded immediately. Clients MUST NOT use tickets for longer than 7 |
1092 | | * days after issuance, regardless of the ticket_lifetime, and MAY delete |
1093 | | * tickets earlier based on local policy. A server MAY treat a ticket as |
1094 | | * valid for a shorter period of time than what is stated in the |
1095 | | * ticket_lifetime. |
1096 | | * |
1097 | | * The hint is capped when a NewSessionTicket is parsed, but a session |
1098 | | * restored with d2i_SSL_SESSION() carries whatever value was serialised. |
1099 | | */ |
1100 | 0 | lifetime = s->session->ext.tick_lifetime_hint; |
1101 | 0 | if (lifetime > 604800) |
1102 | 0 | lifetime = 604800; |
1103 | 0 | if (lifetime == 0 || ossl_time_compare(t, ossl_seconds2time(lifetime)) > 0) |
1104 | 0 | s->ext.tick_age_ok = 0; |
1105 | 0 | else if (agesec != 0 && s->ext.tick_age_ms / (uint32_t)1000 != agesec) |
1106 | 0 | s->ext.tick_age_ok = 0; |
1107 | |
|
1108 | 0 | s->ext.tick_age_checked = 1; |
1109 | 0 | return s->ext.tick_age_ok; |
1110 | 0 | } |
1111 | | |
1112 | | /* |
1113 | | * Mirrors the ticket-resumption gating checks in tls_construct_ctos_psk() so |
1114 | | * that early_data is only advertised when the resumption PSK will actually |
1115 | | * be sent. |
1116 | | */ |
1117 | | static int tls13_check_resumption_psk(SSL_CONNECTION *s, const EVP_MD *handmd) |
1118 | 20.2k | { |
1119 | 20.2k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1120 | 20.2k | const EVP_MD *mdres; |
1121 | 20.2k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
1122 | | |
1123 | 20.2k | if (s->session == NULL |
1124 | 20.2k | || s->session->ssl_version != version1_3 |
1125 | 20.2k | || s->session->ext.ticklen == 0 |
1126 | 0 | || s->session->cipher == NULL) |
1127 | 20.2k | return 0; |
1128 | | |
1129 | 0 | mdres = ssl_md(sctx, s->session->cipher->algorithm2); |
1130 | 0 | if (mdres == NULL) |
1131 | 0 | return 0; |
1132 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) |
1133 | 0 | return 0; |
1134 | 0 | if (tls13_check_tick_lifetime_hint(s) == 0) |
1135 | 0 | return 0; |
1136 | | |
1137 | 0 | return 1; |
1138 | 0 | } |
1139 | | |
1140 | | EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt, |
1141 | | unsigned int context, X509 *x, |
1142 | | size_t chainidx) |
1143 | 20.2k | { |
1144 | 20.2k | #ifndef OPENSSL_NO_PSK |
1145 | 20.2k | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
1146 | 20.2k | #endif /* OPENSSL_NO_PSK */ |
1147 | 20.2k | const unsigned char *id = NULL; |
1148 | 20.2k | size_t idlen = 0; |
1149 | 20.2k | SSL_SESSION *psksess = NULL; |
1150 | 20.2k | SSL_SESSION *edsess = NULL; |
1151 | 20.2k | const EVP_MD *handmd = NULL; |
1152 | 20.2k | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1153 | 20.2k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
1154 | | |
1155 | 20.2k | s->ext.tick_age_checked = 0; |
1156 | | |
1157 | 20.2k | #ifndef OPENSSL_NO_ECH |
1158 | | /* |
1159 | | * If we're attempting ECH and processing the outer CH |
1160 | | * then we only need to check if the extension is to be |
1161 | | * sent or not - any other processing (with side effects) |
1162 | | * happened already for the inner CH. |
1163 | | */ |
1164 | 20.2k | if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) { |
1165 | | /* |
1166 | | * if we called this for inner and did send then |
1167 | | * the following two things should be set, if so, |
1168 | | * then send again in the outer CH. |
1169 | | */ |
1170 | 0 | if (s->ext.early_data == SSL_EARLY_DATA_REJECTED |
1171 | 0 | && s->ext.early_data_ok == 1) { |
1172 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) |
1173 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1174 | 0 | || !WPACKET_close(pkt)) { |
1175 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1176 | 0 | return EXT_RETURN_FAIL; |
1177 | 0 | } |
1178 | 0 | return EXT_RETURN_SENT; |
1179 | 0 | } else { |
1180 | 0 | return EXT_RETURN_NOT_SENT; |
1181 | 0 | } |
1182 | 0 | } |
1183 | 20.2k | #endif |
1184 | 20.2k | if (s->hello_retry_request == SSL_HRR_PENDING) |
1185 | 364 | handmd = ssl_handshake_md(s); |
1186 | | |
1187 | 20.2k | if (s->psk_use_session_cb != NULL |
1188 | 0 | && (!s->psk_use_session_cb(ussl, handmd, &id, &idlen, &psksess) |
1189 | 0 | || (psksess != NULL && psksess->ssl_version != version1_3))) { |
1190 | 0 | SSL_SESSION_free(psksess); |
1191 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1192 | 0 | return EXT_RETURN_FAIL; |
1193 | 0 | } |
1194 | | |
1195 | 20.2k | #ifndef OPENSSL_NO_PSK |
1196 | 20.2k | if (psksess == NULL && s->psk_client_callback != NULL) { |
1197 | 0 | unsigned char psk[PSK_MAX_PSK_LEN]; |
1198 | 0 | size_t psklen = 0; |
1199 | |
|
1200 | 0 | memset(identity, 0, sizeof(identity)); |
1201 | 0 | psklen = s->psk_client_callback(ussl, NULL, |
1202 | 0 | identity, sizeof(identity) - 1, |
1203 | 0 | psk, sizeof(psk)); |
1204 | |
|
1205 | 0 | if (psklen > PSK_MAX_PSK_LEN) { |
1206 | 0 | OPENSSL_cleanse(psk, sizeof(psk)); |
1207 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1208 | 0 | return EXT_RETURN_FAIL; |
1209 | 0 | } |
1210 | 0 | if (psklen > 0) { |
1211 | 0 | const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; |
1212 | 0 | const SSL_CIPHER *cipher; |
1213 | |
|
1214 | 0 | idlen = strlen(identity); |
1215 | 0 | if (idlen > PSK_MAX_IDENTITY_LEN) { |
1216 | 0 | OPENSSL_cleanse(psk, sizeof(psk)); |
1217 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1218 | 0 | return EXT_RETURN_FAIL; |
1219 | 0 | } |
1220 | 0 | id = (unsigned char *)identity; |
1221 | | |
1222 | | /* |
1223 | | * We found a PSK using an old style callback. We don't know |
1224 | | * the digest so we default to SHA256 as per the (D)TLSv1.3 spec |
1225 | | */ |
1226 | 0 | cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s), |
1227 | 0 | tls13_aes128gcmsha256_id); |
1228 | 0 | if (cipher == NULL) { |
1229 | 0 | OPENSSL_cleanse(psk, sizeof(psk)); |
1230 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1231 | 0 | return EXT_RETURN_FAIL; |
1232 | 0 | } |
1233 | | |
1234 | 0 | psksess = SSL_SESSION_new(); |
1235 | |
|
1236 | 0 | if (psksess == NULL |
1237 | 0 | || !SSL_SESSION_set1_master_key(psksess, psk, psklen) |
1238 | 0 | || !SSL_SESSION_set_cipher(psksess, cipher) |
1239 | 0 | || !SSL_SESSION_set_protocol_version(psksess, version1_3)) { |
1240 | 0 | SSL_SESSION_free(psksess); |
1241 | 0 | OPENSSL_cleanse(psk, sizeof(psk)); |
1242 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1243 | 0 | return EXT_RETURN_FAIL; |
1244 | 0 | } |
1245 | 0 | OPENSSL_cleanse(psk, psklen); |
1246 | 0 | } |
1247 | 0 | } |
1248 | 20.2k | #endif /* OPENSSL_NO_PSK */ |
1249 | | |
1250 | 20.2k | SSL_SESSION_free(s->psksession); |
1251 | 20.2k | s->psksession = psksess; |
1252 | 20.2k | if (psksess != NULL) { |
1253 | 0 | OPENSSL_free(s->psksession_id); |
1254 | 0 | s->psksession_id = OPENSSL_memdup(id, idlen); |
1255 | 0 | if (s->psksession_id == NULL) { |
1256 | 0 | s->psksession_id_len = 0; |
1257 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1258 | 0 | return EXT_RETURN_FAIL; |
1259 | 0 | } |
1260 | 0 | s->psksession_id_len = idlen; |
1261 | 0 | } |
1262 | | |
1263 | | /* |
1264 | | * Suppress early_data unless a PSK is available and will be sent. |
1265 | | * |
1266 | | * RFC 9846 4.3.10: When a PSK is used and early data is allowed for that |
1267 | | * PSK, the client can send Application Data in its first flight of |
1268 | | * messages. If the client opts to do so, it MUST supply both the |
1269 | | * "pre_shared_key" and "early_data" extensions. |
1270 | | * |
1271 | | * The PSK used to encrypt the early data MUST be the first PSK listed in |
1272 | | * the client's "pre_shared_key" extension. |
1273 | | */ |
1274 | | /* |
1275 | | * Slot 0 -- the first identity we will offer -- is the only one that can |
1276 | | * key 0-RTT. It is the resumption session when we are offering it, else |
1277 | | * the external psksession. Offer early_data only when that slot-0 PSK is |
1278 | | * itself 0-RTT-capable; never key it off a PSK in a later slot. |
1279 | | */ |
1280 | 20.2k | edsess = tls13_check_resumption_psk(s, handmd) ? s->session : psksess; |
1281 | 20.2k | if (s->early_data_state != SSL_EARLY_DATA_CONNECTING |
1282 | 0 | || edsess == NULL |
1283 | 20.2k | || edsess->ext.max_early_data == 0) { |
1284 | 20.2k | s->max_early_data = 0; |
1285 | 20.2k | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) { |
1286 | 0 | s->ext.early_data_suppressed = 1; |
1287 | 0 | s->ext.early_data = SSL_EARLY_DATA_REJECTED; |
1288 | | /* |
1289 | | * We report REJECTED (not NOT_SENT), so |
1290 | | * SSL_export_keying_material_early() stays callable as it is for a |
1291 | | * server-rejected attempt -- but no early exporter secret was |
1292 | | * derived here. Randomise it so any such export yields a harmless |
1293 | | * per-connection orphan, not an all-zero (predictable) or stale |
1294 | | * (prior-handshake) value. |
1295 | | */ |
1296 | 0 | if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, |
1297 | 0 | s->early_exporter_master_secret, |
1298 | 0 | sizeof(s->early_exporter_master_secret), 0) |
1299 | 0 | <= 0) { |
1300 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1301 | 0 | return EXT_RETURN_FAIL; |
1302 | 0 | } |
1303 | 0 | } |
1304 | 20.2k | s->early_data_state = SSL_EARLY_DATA_NONE; |
1305 | 20.2k | return EXT_RETURN_NOT_SENT; |
1306 | 20.2k | } |
1307 | 0 | s->max_early_data = edsess->ext.max_early_data; |
1308 | | /* |
1309 | | * Freeze slot 0 (candidate_at(0)) so the binder, the early-key derivation, |
1310 | | * the early exporter, the byte-budget lookup and the post-ServerHello fixup |
1311 | | * all key off the actual first-offered PSK rather than guessing the source |
1312 | | * from s->session->ext.max_early_data. Held (up-ref'd) so it stays valid |
1313 | | * across the swap that later folds a selected psksession into s->session. |
1314 | | */ |
1315 | 0 | SSL_SESSION_free(s->ext.early_data_session); |
1316 | 0 | s->ext.early_data_session = edsess; |
1317 | 0 | if (!SSL_SESSION_up_ref(edsess)) { |
1318 | 0 | s->ext.early_data_session = NULL; |
1319 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1320 | 0 | return EXT_RETURN_FAIL; |
1321 | 0 | } |
1322 | | |
1323 | 0 | if (edsess->ext.hostname != NULL) { |
1324 | 0 | if (s->ext.hostname == NULL |
1325 | 0 | || (s->ext.hostname != NULL |
1326 | 0 | && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) { |
1327 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
1328 | 0 | SSL_R_INCONSISTENT_EARLY_DATA_SNI); |
1329 | 0 | return EXT_RETURN_FAIL; |
1330 | 0 | } |
1331 | 0 | } |
1332 | | |
1333 | 0 | if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) { |
1334 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN); |
1335 | 0 | return EXT_RETURN_FAIL; |
1336 | 0 | } |
1337 | | |
1338 | | /* |
1339 | | * Verify that we are offering an ALPN protocol consistent with the early |
1340 | | * data. |
1341 | | */ |
1342 | 0 | if (edsess->ext.alpn_selected != NULL) { |
1343 | 0 | PACKET prots, alpnpkt; |
1344 | 0 | int found = 0; |
1345 | |
|
1346 | 0 | if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) { |
1347 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1348 | 0 | return EXT_RETURN_FAIL; |
1349 | 0 | } |
1350 | 0 | while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) { |
1351 | 0 | if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected, |
1352 | 0 | edsess->ext.alpn_selected_len)) { |
1353 | 0 | found = 1; |
1354 | 0 | break; |
1355 | 0 | } |
1356 | 0 | } |
1357 | 0 | if (!found) { |
1358 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
1359 | 0 | SSL_R_INCONSISTENT_EARLY_DATA_ALPN); |
1360 | 0 | return EXT_RETURN_FAIL; |
1361 | 0 | } |
1362 | 0 | } |
1363 | | |
1364 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) |
1365 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1366 | 0 | || !WPACKET_close(pkt)) { |
1367 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1368 | 0 | return EXT_RETURN_FAIL; |
1369 | 0 | } |
1370 | | |
1371 | | /* |
1372 | | * We set this to rejected here. Later, if the server acknowledges the |
1373 | | * extension, we set it to accepted. |
1374 | | */ |
1375 | 0 | s->ext.early_data = SSL_EARLY_DATA_REJECTED; |
1376 | 0 | s->ext.early_data_ok = 1; |
1377 | |
|
1378 | 0 | return EXT_RETURN_SENT; |
1379 | 0 | } |
1380 | | |
1381 | | /* |
1382 | | * Construct the pre_shared_key extension |
1383 | | */ |
1384 | | EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt, |
1385 | | unsigned int context, |
1386 | | X509 *x, size_t chainidx) |
1387 | 20.2k | { |
1388 | 20.2k | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
1389 | 20.2k | uint32_t agems = 0; |
1390 | 20.2k | size_t binderoffset, msglen, resbinder_end_offset = 0, pskbinder_end_offset = 0; |
1391 | 20.2k | int reshashsize = 0, pskhashsize = 0; |
1392 | 20.2k | unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL; |
1393 | 20.2k | const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL; |
1394 | 20.2k | int dores = 0; |
1395 | 20.2k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1396 | 20.2k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
1397 | | |
1398 | 20.2k | s->ext.tick_identity = 0; |
1399 | | |
1400 | | /* |
1401 | | * If this is an incompatible or new session then we have nothing to resume |
1402 | | * so don't add this extension. |
1403 | | */ |
1404 | 20.2k | if (s->session->ssl_version != version1_3 |
1405 | 20.2k | || (s->session->ext.ticklen == 0 && s->psksession == NULL)) |
1406 | 20.2k | return EXT_RETURN_NOT_SENT; |
1407 | | |
1408 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) |
1409 | 0 | handmd = ssl_handshake_md(s); |
1410 | |
|
1411 | 0 | if (s->session->ext.ticklen != 0) { |
1412 | | /* Get the digest associated with the ciphersuite in the session */ |
1413 | 0 | if (s->session->cipher == NULL) { |
1414 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1415 | 0 | return EXT_RETURN_FAIL; |
1416 | 0 | } |
1417 | 0 | mdres = ssl_md(sctx, s->session->cipher->algorithm2); |
1418 | 0 | if (mdres == NULL) { |
1419 | | /* |
1420 | | * Don't recognize this cipher so we can't use the session. |
1421 | | * Ignore it |
1422 | | */ |
1423 | 0 | goto dopsksess; |
1424 | 0 | } |
1425 | | |
1426 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) { |
1427 | | /* |
1428 | | * Selected ciphersuite hash does not match the hash for the session |
1429 | | * so we can't use it. |
1430 | | */ |
1431 | 0 | goto dopsksess; |
1432 | 0 | } |
1433 | | |
1434 | 0 | #ifndef OPENSSL_NO_ECH |
1435 | | /* |
1436 | | * When doing ECH, we get here twice (for inner then outer). The |
1437 | | * 2nd time (for outer) we can skip some checks as we know how |
1438 | | * those went last time. |
1439 | | */ |
1440 | 0 | if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) { |
1441 | 0 | s->ext.tick_identity = s->ext.ech.tick_identity; |
1442 | 0 | dores = (s->ext.tick_identity > 0); |
1443 | 0 | goto dopsksess; |
1444 | 0 | } |
1445 | 0 | #endif |
1446 | | |
1447 | 0 | if (tls13_check_tick_lifetime_hint(s) == 0) |
1448 | 0 | goto dopsksess; |
1449 | | /* tls13_check_tick_lifetime_hint() updates the tick_age_ms value. */ |
1450 | 0 | agems = s->ext.tick_age_ms; |
1451 | | |
1452 | | /* |
1453 | | * Obfuscate the age. Overflow here is fine, this addition is supposed |
1454 | | * to be mod 2^32. |
1455 | | */ |
1456 | 0 | agems += s->session->ext.tick_age_add; |
1457 | |
|
1458 | 0 | reshashsize = EVP_MD_get_size(mdres); |
1459 | 0 | if (reshashsize <= 0) |
1460 | 0 | goto dopsksess; |
1461 | 0 | s->ext.tick_identity++; |
1462 | 0 | #ifndef OPENSSL_NO_ECH |
1463 | | /* stash this for re-use in outer CH */ |
1464 | 0 | if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 1) |
1465 | 0 | s->ext.ech.tick_identity = s->ext.tick_identity; |
1466 | 0 | #endif |
1467 | 0 | dores = 1; |
1468 | 0 | } |
1469 | | |
1470 | 0 | dopsksess: |
1471 | 0 | if (!dores && s->psksession == NULL) |
1472 | 0 | return EXT_RETURN_NOT_SENT; |
1473 | | |
1474 | 0 | if (s->psksession != NULL) { |
1475 | 0 | mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2); |
1476 | 0 | if (mdpsk == NULL) { |
1477 | | /* |
1478 | | * Don't recognize this cipher so we can't use the session. |
1479 | | * If this happens it's an application bug. |
1480 | | */ |
1481 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1482 | 0 | return EXT_RETURN_FAIL; |
1483 | 0 | } |
1484 | | |
1485 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) { |
1486 | | /* |
1487 | | * Selected ciphersuite hash does not match the hash for the PSK |
1488 | | * session. This is an application bug. |
1489 | | */ |
1490 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1491 | 0 | return EXT_RETURN_FAIL; |
1492 | 0 | } |
1493 | | |
1494 | 0 | pskhashsize = EVP_MD_get_size(mdpsk); |
1495 | 0 | if (pskhashsize <= 0) { |
1496 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1497 | 0 | return EXT_RETURN_FAIL; |
1498 | 0 | } |
1499 | 0 | } |
1500 | | |
1501 | | /* Create the extension, but skip over the binder for now */ |
1502 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk) |
1503 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1504 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
1505 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1506 | 0 | return EXT_RETURN_FAIL; |
1507 | 0 | } |
1508 | | |
1509 | 0 | #ifndef OPENSSL_NO_ECH |
1510 | | /* |
1511 | | * For ECH if we're processing the outer CH and the inner CH |
1512 | | * has a PSK, then we want to send a GREASE PSK in the outer. |
1513 | | * We'll do that by just replacing the ticket value itself |
1514 | | * with random values of the same length. |
1515 | | */ |
1516 | 0 | if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) { |
1517 | 0 | unsigned char *rndbuf = NULL, *rndbufp = NULL; |
1518 | 0 | size_t totalrndsize = 0; |
1519 | |
|
1520 | 0 | totalrndsize = s->session->ext.ticklen |
1521 | 0 | + sizeof(agems) |
1522 | 0 | + s->psksession_id_len |
1523 | 0 | + reshashsize |
1524 | 0 | + pskhashsize; |
1525 | 0 | rndbuf = OPENSSL_malloc(totalrndsize); |
1526 | 0 | if (rndbuf == NULL) { |
1527 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1528 | 0 | return EXT_RETURN_FAIL; |
1529 | 0 | } |
1530 | | /* for outer CH allocate a similar sized random value */ |
1531 | 0 | if (RAND_bytes_ex(sctx->libctx, rndbuf, totalrndsize, 0) <= 0) { |
1532 | 0 | OPENSSL_free(rndbuf); |
1533 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1534 | 0 | return EXT_RETURN_FAIL; |
1535 | 0 | } |
1536 | | /* set agems from random buffer */ |
1537 | 0 | rndbufp = rndbuf; |
1538 | 0 | memcpy(&agems, rndbufp, sizeof(agems)); |
1539 | 0 | rndbufp += sizeof(agems); |
1540 | 0 | if (dores != 0) { |
1541 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, rndbufp, |
1542 | 0 | s->session->ext.ticklen) |
1543 | 0 | || !WPACKET_put_bytes_u32(pkt, agems)) { |
1544 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1545 | 0 | OPENSSL_free(rndbuf); |
1546 | 0 | return EXT_RETURN_FAIL; |
1547 | 0 | } |
1548 | 0 | rndbufp += s->session->ext.ticklen; |
1549 | 0 | } |
1550 | 0 | if (s->psksession != NULL) { |
1551 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, rndbufp, s->psksession_id_len) |
1552 | 0 | || !WPACKET_put_bytes_u32(pkt, 0)) { |
1553 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1554 | 0 | OPENSSL_free(rndbuf); |
1555 | 0 | return EXT_RETURN_FAIL; |
1556 | 0 | } |
1557 | 0 | rndbufp += s->psksession_id_len; |
1558 | 0 | } |
1559 | 0 | if (!WPACKET_close(pkt) |
1560 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1561 | 0 | || (dores == 1 |
1562 | 0 | && !WPACKET_sub_memcpy_u8(pkt, rndbufp, reshashsize)) |
1563 | 0 | || (s->psksession != NULL |
1564 | 0 | && !WPACKET_sub_memcpy_u8(pkt, rndbufp, pskhashsize)) |
1565 | 0 | || !WPACKET_close(pkt) |
1566 | 0 | || !WPACKET_close(pkt)) { |
1567 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1568 | 0 | OPENSSL_free(rndbuf); |
1569 | 0 | return EXT_RETURN_FAIL; |
1570 | 0 | } |
1571 | 0 | OPENSSL_free(rndbuf); |
1572 | 0 | return EXT_RETURN_SENT; |
1573 | 0 | } |
1574 | 0 | #endif /* OPENSSL_NO_ECH */ |
1575 | 0 | if (dores) { |
1576 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, |
1577 | 0 | s->session->ext.ticklen) |
1578 | 0 | || !WPACKET_put_bytes_u32(pkt, agems)) { |
1579 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1580 | 0 | return EXT_RETURN_FAIL; |
1581 | 0 | } |
1582 | 0 | } |
1583 | | |
1584 | 0 | if (s->psksession != NULL) { |
1585 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id, |
1586 | 0 | s->psksession_id_len) |
1587 | 0 | || !WPACKET_put_bytes_u32(pkt, 0)) { |
1588 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1589 | 0 | return EXT_RETURN_FAIL; |
1590 | 0 | } |
1591 | 0 | s->ext.tick_identity++; |
1592 | 0 | } |
1593 | | |
1594 | 0 | if (!WPACKET_close(pkt) |
1595 | 0 | || !WPACKET_get_total_written(pkt, &binderoffset) |
1596 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1597 | 0 | || (dores |
1598 | 0 | && (!WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, NULL) |
1599 | 0 | || !WPACKET_get_total_written(pkt, &resbinder_end_offset))) |
1600 | 0 | || (s->psksession != NULL |
1601 | 0 | && (!WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, NULL) |
1602 | 0 | || !WPACKET_get_total_written(pkt, &pskbinder_end_offset))) |
1603 | 0 | || !WPACKET_close(pkt) |
1604 | 0 | || !WPACKET_close(pkt) |
1605 | 0 | || !WPACKET_get_total_written(pkt, &msglen) |
1606 | | /* |
1607 | | * We need to fill in all the sub-packet lengths now so we can |
1608 | | * calculate the HMAC of the message up to the binders |
1609 | | */ |
1610 | 0 | || !WPACKET_fill_lengths(pkt)) { |
1611 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1612 | 0 | return EXT_RETURN_FAIL; |
1613 | 0 | } |
1614 | | |
1615 | 0 | msgstart = WPACKET_get_curr(pkt) - msglen; |
1616 | | /* Allocating either binder may relocate the packet's backing buffer. */ |
1617 | 0 | if (dores) |
1618 | 0 | resbinder = msgstart + resbinder_end_offset - reshashsize; |
1619 | 0 | if (s->psksession != NULL) |
1620 | 0 | pskbinder = msgstart + pskbinder_end_offset - pskhashsize; |
1621 | |
|
1622 | 0 | if (dores && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL, resbinder, s->session, 1, 0) != 1) { |
1623 | | /* SSLfatal() already called */ |
1624 | 0 | return EXT_RETURN_FAIL; |
1625 | 0 | } |
1626 | | |
1627 | 0 | if (s->psksession != NULL |
1628 | 0 | && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL, |
1629 | 0 | pskbinder, s->psksession, 1, 1) |
1630 | 0 | != 1) { |
1631 | | /* SSLfatal() already called */ |
1632 | 0 | return EXT_RETURN_FAIL; |
1633 | 0 | } |
1634 | | |
1635 | 0 | return EXT_RETURN_SENT; |
1636 | | #else |
1637 | | return EXT_RETURN_NOT_SENT; |
1638 | | #endif |
1639 | 0 | } |
1640 | | |
1641 | | EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt, |
1642 | | ossl_unused unsigned int context, |
1643 | | ossl_unused X509 *x, |
1644 | | ossl_unused size_t chainidx) |
1645 | 56.4k | { |
1646 | 56.4k | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
1647 | 56.4k | if (!s->pha_enabled) |
1648 | 56.4k | return EXT_RETURN_NOT_SENT; |
1649 | 0 | #ifndef OPENSSL_NO_ECH |
1650 | 0 | ECH_SAME_EXT(s, context, pkt) |
1651 | 0 | #endif |
1652 | | |
1653 | | /* construct extension - 0 length, no contents */ |
1654 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth) |
1655 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1656 | 0 | || !WPACKET_close(pkt)) { |
1657 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1658 | 0 | return EXT_RETURN_FAIL; |
1659 | 0 | } |
1660 | | |
1661 | 0 | s->post_handshake_auth = SSL_PHA_EXT_SENT; |
1662 | |
|
1663 | 0 | return EXT_RETURN_SENT; |
1664 | | #else |
1665 | | return EXT_RETURN_NOT_SENT; |
1666 | | #endif |
1667 | 0 | } |
1668 | | |
1669 | | /* |
1670 | | * Parse the server's renegotiation binding and abort if it's not right |
1671 | | */ |
1672 | | int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt, |
1673 | | unsigned int context, |
1674 | | X509 *x, size_t chainidx) |
1675 | 51.9k | { |
1676 | 51.9k | size_t expected_len = s->s3.previous_client_finished_len |
1677 | 51.9k | + s->s3.previous_server_finished_len; |
1678 | 51.9k | size_t ilen; |
1679 | 51.9k | const unsigned char *data; |
1680 | | |
1681 | | /* Check for logic errors */ |
1682 | 51.9k | if (!ossl_assert(expected_len == 0 |
1683 | 51.9k | || s->s3.previous_client_finished_len != 0) |
1684 | 51.9k | || !ossl_assert(expected_len == 0 |
1685 | 51.9k | || s->s3.previous_server_finished_len != 0)) { |
1686 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1687 | 0 | return 0; |
1688 | 0 | } |
1689 | | |
1690 | | /* Parse the length byte */ |
1691 | 51.9k | if (!PACKET_get_1_len(pkt, &ilen)) { |
1692 | 13 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
1693 | 13 | return 0; |
1694 | 13 | } |
1695 | | |
1696 | | /* Consistency check */ |
1697 | 51.9k | if (PACKET_remaining(pkt) != ilen) { |
1698 | 84 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
1699 | 84 | return 0; |
1700 | 84 | } |
1701 | | |
1702 | | /* Check that the extension matches */ |
1703 | 51.8k | if (ilen != expected_len) { |
1704 | 27 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
1705 | 27 | return 0; |
1706 | 27 | } |
1707 | | |
1708 | 51.8k | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len) |
1709 | 51.8k | || memcmp(data, s->s3.previous_client_finished, |
1710 | 51.8k | s->s3.previous_client_finished_len) |
1711 | 51.8k | != 0) { |
1712 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
1713 | 0 | return 0; |
1714 | 0 | } |
1715 | | |
1716 | 51.8k | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len) |
1717 | 51.8k | || memcmp(data, s->s3.previous_server_finished, |
1718 | 51.8k | s->s3.previous_server_finished_len) |
1719 | 51.8k | != 0) { |
1720 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
1721 | 0 | return 0; |
1722 | 0 | } |
1723 | 51.8k | s->s3.send_connection_binding = 1; |
1724 | | |
1725 | 51.8k | return 1; |
1726 | 51.8k | } |
1727 | | |
1728 | | /* Parse the server's max fragment len extension packet */ |
1729 | | int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt, |
1730 | | unsigned int context, |
1731 | | X509 *x, size_t chainidx) |
1732 | 0 | { |
1733 | 0 | unsigned int value; |
1734 | |
|
1735 | 0 | if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { |
1736 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1737 | 0 | return 0; |
1738 | 0 | } |
1739 | | |
1740 | | /* |value| should contains a valid max-fragment-length code. */ |
1741 | 0 | if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { |
1742 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1743 | 0 | SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
1744 | 0 | return 0; |
1745 | 0 | } |
1746 | | |
1747 | | /* Must be the same value as client-configured one who was sent to server */ |
1748 | | /*- |
1749 | | * RFC 6066: if a client receives a maximum fragment length negotiation |
1750 | | * response that differs from the length it requested, ... |
1751 | | * It must abort with SSL_AD_ILLEGAL_PARAMETER alert |
1752 | | */ |
1753 | 0 | if (value != s->ext.max_fragment_len_mode) { |
1754 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1755 | 0 | SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
1756 | 0 | return 0; |
1757 | 0 | } |
1758 | | |
1759 | | /* |
1760 | | * Maximum Fragment Length Negotiation succeeded. |
1761 | | * The negotiated Maximum Fragment Length is binding now. |
1762 | | */ |
1763 | 0 | s->session->ext.max_fragment_len_mode = value; |
1764 | |
|
1765 | 0 | return 1; |
1766 | 0 | } |
1767 | | |
1768 | | int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt, |
1769 | | unsigned int context, |
1770 | | X509 *x, size_t chainidx) |
1771 | 6.55k | { |
1772 | 6.55k | char *eff_sni = s->ext.hostname; |
1773 | | |
1774 | 6.55k | #ifndef OPENSSL_NO_ECH |
1775 | | /* if we tried ECH and failed, the outer is what's expected */ |
1776 | 6.55k | if (s->ext.ech.es != NULL && s->ext.ech.success == 0) |
1777 | 0 | eff_sni = s->ext.ech.outer_hostname; |
1778 | 6.55k | #endif |
1779 | 6.55k | if (eff_sni == NULL) { |
1780 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1781 | 0 | return 0; |
1782 | 0 | } |
1783 | 6.55k | if (PACKET_remaining(pkt) > 0) { |
1784 | 10 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1785 | 10 | return 0; |
1786 | 10 | } |
1787 | 6.54k | if (!s->hit) { |
1788 | 6.54k | if (s->session->ext.hostname != NULL) { |
1789 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1790 | 0 | return 0; |
1791 | 0 | } |
1792 | 6.54k | s->session->ext.hostname = OPENSSL_strdup(eff_sni); |
1793 | 6.54k | if (s->session->ext.hostname == NULL) { |
1794 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1795 | 0 | return 0; |
1796 | 0 | } |
1797 | 6.54k | } |
1798 | 6.54k | return 1; |
1799 | 6.54k | } |
1800 | | |
1801 | | int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt, |
1802 | | unsigned int context, |
1803 | | X509 *x, size_t chainidx) |
1804 | 10.4k | { |
1805 | 10.4k | SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); |
1806 | | |
1807 | 10.4k | if (s->ext.session_ticket_cb != NULL && !s->ext.session_ticket_cb(ssl, PACKET_data(pkt), (int)PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) { |
1808 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); |
1809 | 0 | return 0; |
1810 | 0 | } |
1811 | | |
1812 | 10.4k | if (!tls_use_ticket(s)) { |
1813 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1814 | 0 | return 0; |
1815 | 0 | } |
1816 | 10.4k | if (PACKET_remaining(pkt) > 0) { |
1817 | 16 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1818 | 16 | return 0; |
1819 | 16 | } |
1820 | | |
1821 | 10.4k | s->ext.ticket_expected = 1; |
1822 | | |
1823 | 10.4k | return 1; |
1824 | 10.4k | } |
1825 | | |
1826 | | #ifndef OPENSSL_NO_OCSP |
1827 | | int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt, |
1828 | | unsigned int context, |
1829 | | X509 *x, size_t chainidx) |
1830 | 2 | { |
1831 | 2 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { |
1832 | | /* We ignore this if the server sends a CertificateRequest */ |
1833 | 2 | return 1; |
1834 | 2 | } |
1835 | | |
1836 | | /* |
1837 | | * MUST only be sent if we've requested a status |
1838 | | * request message. In (D)TLS <= 1.2 it must also be empty. |
1839 | | */ |
1840 | 0 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { |
1841 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1842 | 0 | return 0; |
1843 | 0 | } |
1844 | 0 | if (!SSL_CONNECTION_IS_VERSION13(s) && PACKET_remaining(pkt) > 0) { |
1845 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1846 | 0 | return 0; |
1847 | 0 | } |
1848 | | |
1849 | 0 | if (SSL_CONNECTION_IS_VERSION13(s)) { |
1850 | | /* SSLfatal() already called */ |
1851 | 0 | return tls_process_cert_status_body(s, chainidx, pkt); |
1852 | 0 | } |
1853 | | |
1854 | | /* Set flag to expect CertificateStatus message */ |
1855 | 0 | s->ext.status_expected = 1; |
1856 | |
|
1857 | 0 | return 1; |
1858 | 0 | } |
1859 | | #endif |
1860 | | |
1861 | | #ifndef OPENSSL_NO_CT |
1862 | | int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1863 | | X509 *x, size_t chainidx) |
1864 | 31 | { |
1865 | 31 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { |
1866 | | /* We ignore this if the server sends it in a CertificateRequest */ |
1867 | 6 | return 1; |
1868 | 6 | } |
1869 | | |
1870 | | /* |
1871 | | * Only take it if we asked for it - i.e if there is no CT validation |
1872 | | * callback set, then a custom extension MAY be processing it, so we |
1873 | | * need to let control continue to flow to that. |
1874 | | */ |
1875 | 25 | if (s->ct_validation_callback != NULL) { |
1876 | 0 | size_t size = PACKET_remaining(pkt); |
1877 | | |
1878 | | /* Simply copy it off for later processing */ |
1879 | 0 | OPENSSL_free(s->ext.scts); |
1880 | 0 | s->ext.scts = NULL; |
1881 | |
|
1882 | 0 | s->ext.scts_len = (uint16_t)size; |
1883 | 0 | if (size > 0) { |
1884 | 0 | s->ext.scts = OPENSSL_malloc(size); |
1885 | 0 | if (s->ext.scts == NULL) { |
1886 | 0 | s->ext.scts_len = 0; |
1887 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
1888 | 0 | return 0; |
1889 | 0 | } |
1890 | 0 | if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) { |
1891 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1892 | 0 | return 0; |
1893 | 0 | } |
1894 | 0 | } |
1895 | 25 | } else { |
1896 | 25 | ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0 |
1897 | 25 | ? ENDPOINT_CLIENT |
1898 | 25 | : ENDPOINT_BOTH; |
1899 | | |
1900 | | /* |
1901 | | * If we didn't ask for it then there must be a custom extension, |
1902 | | * otherwise this is unsolicited. |
1903 | | */ |
1904 | 25 | if (custom_ext_find(&s->cert->custext, role, |
1905 | 25 | TLSEXT_TYPE_signed_certificate_timestamp, |
1906 | 25 | NULL) |
1907 | 25 | == NULL) { |
1908 | 25 | SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1909 | 25 | return 0; |
1910 | 25 | } |
1911 | | |
1912 | 0 | if (!custom_ext_parse(s, context, |
1913 | 0 | TLSEXT_TYPE_signed_certificate_timestamp, |
1914 | 0 | PACKET_data(pkt), PACKET_remaining(pkt), |
1915 | 0 | x, chainidx)) { |
1916 | | /* SSLfatal already called */ |
1917 | 0 | return 0; |
1918 | 0 | } |
1919 | 0 | } |
1920 | | |
1921 | 0 | return 1; |
1922 | 25 | } |
1923 | | #endif |
1924 | | |
1925 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
1926 | | /* |
1927 | | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
1928 | | * elements of zero length are allowed and the set of elements must exactly |
1929 | | * fill the length of the block. Returns 1 on success or 0 on failure. |
1930 | | */ |
1931 | | static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt) |
1932 | 0 | { |
1933 | 0 | PACKET tmp_protocol; |
1934 | |
|
1935 | 0 | while (PACKET_remaining(pkt)) { |
1936 | 0 | if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol) |
1937 | 0 | || PACKET_remaining(&tmp_protocol) == 0) { |
1938 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1939 | 0 | return 0; |
1940 | 0 | } |
1941 | 0 | } |
1942 | | |
1943 | 0 | return 1; |
1944 | 0 | } |
1945 | | |
1946 | | int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1947 | | X509 *x, size_t chainidx) |
1948 | 0 | { |
1949 | 0 | unsigned char *selected; |
1950 | 0 | unsigned char selected_len; |
1951 | 0 | PACKET tmppkt; |
1952 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1953 | | |
1954 | | /* Check if we are in a renegotiation. If so ignore this extension */ |
1955 | 0 | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
1956 | 0 | return 1; |
1957 | | |
1958 | | /* We must have requested it. */ |
1959 | 0 | if (sctx->ext.npn_select_cb == NULL) { |
1960 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1961 | 0 | return 0; |
1962 | 0 | } |
1963 | | |
1964 | | /* The data must be valid */ |
1965 | 0 | tmppkt = *pkt; |
1966 | 0 | if (!ssl_next_proto_validate(s, &tmppkt)) { |
1967 | | /* SSLfatal() already called */ |
1968 | 0 | return 0; |
1969 | 0 | } |
1970 | 0 | if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_USER_SSL(s), |
1971 | 0 | &selected, &selected_len, |
1972 | 0 | PACKET_data(pkt), (unsigned int)PACKET_remaining(pkt), |
1973 | 0 | sctx->ext.npn_select_cb_arg) |
1974 | 0 | != SSL_TLSEXT_ERR_OK |
1975 | 0 | || selected_len == 0) { |
1976 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); |
1977 | 0 | return 0; |
1978 | 0 | } |
1979 | | |
1980 | | /* |
1981 | | * Could be non-NULL if server has sent multiple NPN extensions in |
1982 | | * a single Serverhello |
1983 | | */ |
1984 | 0 | OPENSSL_free(s->ext.npn); |
1985 | 0 | s->ext.npn = OPENSSL_malloc(selected_len); |
1986 | 0 | if (s->ext.npn == NULL) { |
1987 | 0 | s->ext.npn_len = 0; |
1988 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1989 | 0 | return 0; |
1990 | 0 | } |
1991 | | |
1992 | 0 | memcpy(s->ext.npn, selected, selected_len); |
1993 | 0 | s->ext.npn_len = selected_len; |
1994 | 0 | s->s3.npn_seen = 1; |
1995 | |
|
1996 | 0 | return 1; |
1997 | 0 | } |
1998 | | #endif |
1999 | | |
2000 | | int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
2001 | | X509 *x, size_t chainidx) |
2002 | 21.7k | { |
2003 | 21.7k | size_t len; |
2004 | 21.7k | PACKET confpkt, protpkt; |
2005 | 21.7k | int valid = 0; |
2006 | | |
2007 | | /* We must have requested it. */ |
2008 | 21.7k | if (!s->s3.alpn_sent) { |
2009 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
2010 | 0 | return 0; |
2011 | 0 | } |
2012 | | /*- |
2013 | | * The extension data consists of: |
2014 | | * uint16 list_length |
2015 | | * uint8 proto_length; |
2016 | | * uint8 proto[proto_length]; |
2017 | | */ |
2018 | 21.7k | if (!PACKET_get_net_2_len(pkt, &len) |
2019 | 21.7k | || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len) |
2020 | 21.7k | || PACKET_remaining(pkt) != len) { |
2021 | 65 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2022 | 65 | return 0; |
2023 | 65 | } |
2024 | | |
2025 | | /* It must be a protocol that we sent */ |
2026 | 21.6k | if (!PACKET_buf_init(&confpkt, s->ext.alpn, s->ext.alpn_len)) { |
2027 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2028 | 0 | return 0; |
2029 | 0 | } |
2030 | 21.7k | while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) { |
2031 | 21.6k | if (PACKET_remaining(&protpkt) != len) |
2032 | 6 | continue; |
2033 | 21.6k | if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) { |
2034 | | /* Valid protocol found */ |
2035 | 21.6k | valid = 1; |
2036 | 21.6k | break; |
2037 | 21.6k | } |
2038 | 21.6k | } |
2039 | | |
2040 | 21.6k | if (!valid) { |
2041 | | /* The protocol sent from the server does not match one we advertised */ |
2042 | 75 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2043 | 75 | return 0; |
2044 | 75 | } |
2045 | | |
2046 | 21.6k | OPENSSL_free(s->s3.alpn_selected); |
2047 | 21.6k | s->s3.alpn_selected = OPENSSL_malloc(len); |
2048 | 21.6k | if (s->s3.alpn_selected == NULL) { |
2049 | 0 | s->s3.alpn_selected_len = 0; |
2050 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2051 | 0 | return 0; |
2052 | 0 | } |
2053 | 21.6k | if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) { |
2054 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2055 | 0 | return 0; |
2056 | 0 | } |
2057 | 21.6k | s->s3.alpn_selected_len = len; |
2058 | | |
2059 | 21.6k | if (s->session->ext.alpn_selected == NULL |
2060 | 0 | || s->session->ext.alpn_selected_len != len |
2061 | 0 | || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len) |
2062 | 21.6k | != 0) { |
2063 | | /* ALPN not consistent with the old session so cannot use early_data */ |
2064 | 21.6k | s->ext.early_data_ok = 0; |
2065 | 21.6k | } |
2066 | 21.6k | if (!s->hit) { |
2067 | | /* |
2068 | | * This is a new session and so alpn_selected should have been |
2069 | | * initialised to NULL. We should update it with the selected ALPN. |
2070 | | */ |
2071 | 21.6k | if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { |
2072 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2073 | 0 | return 0; |
2074 | 0 | } |
2075 | 21.6k | s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); |
2076 | 21.6k | if (s->session->ext.alpn_selected == NULL) { |
2077 | 0 | s->session->ext.alpn_selected_len = 0; |
2078 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2079 | 0 | return 0; |
2080 | 0 | } |
2081 | 21.6k | s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; |
2082 | 21.6k | } |
2083 | | |
2084 | 21.6k | return 1; |
2085 | 21.6k | } |
2086 | | |
2087 | | #ifndef OPENSSL_NO_SRTP |
2088 | | int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt, |
2089 | | unsigned int context, X509 *x, size_t chainidx) |
2090 | 0 | { |
2091 | 0 | unsigned int id, ct, mki; |
2092 | 0 | int i; |
2093 | 0 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt; |
2094 | 0 | SRTP_PROTECTION_PROFILE *prof; |
2095 | |
|
2096 | 0 | if (!PACKET_get_net_2(pkt, &ct) || ct != 2 |
2097 | 0 | || !PACKET_get_net_2(pkt, &id) |
2098 | 0 | || !PACKET_get_1(pkt, &mki) |
2099 | 0 | || PACKET_remaining(pkt) != 0) { |
2100 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
2101 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
2102 | 0 | return 0; |
2103 | 0 | } |
2104 | | |
2105 | 0 | if (mki != 0) { |
2106 | | /* Must be no MKI, since we never offer one */ |
2107 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE); |
2108 | 0 | return 0; |
2109 | 0 | } |
2110 | | |
2111 | | /* Throw an error if the server gave us an unsolicited extension */ |
2112 | 0 | clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s)); |
2113 | 0 | if (clnt == NULL) { |
2114 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_SRTP_PROFILES); |
2115 | 0 | return 0; |
2116 | 0 | } |
2117 | | |
2118 | | /* |
2119 | | * Check to see if the server gave us something we support (and |
2120 | | * presumably offered) |
2121 | | */ |
2122 | 0 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) { |
2123 | 0 | prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); |
2124 | |
|
2125 | 0 | if (prof->id == id) { |
2126 | 0 | s->srtp_profile = prof; |
2127 | 0 | return 1; |
2128 | 0 | } |
2129 | 0 | } |
2130 | | |
2131 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2132 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
2133 | 0 | return 0; |
2134 | 0 | } |
2135 | | #endif |
2136 | | |
2137 | | int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
2138 | | X509 *x, size_t chainidx) |
2139 | 3.87k | { |
2140 | | /* Ignore if inappropriate ciphersuite */ |
2141 | 3.87k | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) |
2142 | 3.87k | && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD |
2143 | 2.56k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4 |
2144 | 2.56k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT |
2145 | 2.56k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12 |
2146 | 2.56k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA |
2147 | 2.56k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK) |
2148 | 2.56k | s->ext.use_etm = 1; |
2149 | | |
2150 | 3.87k | return 1; |
2151 | 3.87k | } |
2152 | | |
2153 | | int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
2154 | | X509 *x, size_t chainidx) |
2155 | 7.89k | { |
2156 | 7.89k | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
2157 | 0 | return 1; |
2158 | 7.89k | s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
2159 | 7.89k | if (!s->hit) |
2160 | 7.88k | s->session->flags |= SSL_SESS_FLAG_EXTMS; |
2161 | | |
2162 | 7.89k | return 1; |
2163 | 7.89k | } |
2164 | | |
2165 | | int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt, |
2166 | | unsigned int context, |
2167 | | X509 *x, size_t chainidx) |
2168 | 9.21k | { |
2169 | 9.21k | unsigned int version; |
2170 | 9.21k | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION; |
2171 | | |
2172 | 9.21k | if (!PACKET_get_net_2(pkt, &version) |
2173 | 9.18k | || PACKET_remaining(pkt) != 0) { |
2174 | 47 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2175 | 47 | return 0; |
2176 | 47 | } |
2177 | | |
2178 | | /* |
2179 | | * The only protocol version we support which is valid in this extension in |
2180 | | * a ServerHello is (D)TLSv1.3 therefore we shouldn't be getting anything else. |
2181 | | */ |
2182 | 9.17k | if ((int)version != version1_3) { |
2183 | 96 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2184 | 96 | SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
2185 | 96 | return 0; |
2186 | 96 | } |
2187 | | |
2188 | | /* We ignore this extension for HRRs except to sanity check it */ |
2189 | 9.07k | if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) |
2190 | 442 | return 1; |
2191 | | |
2192 | | /* We just set it here. We validate it in ssl_choose_client_version */ |
2193 | 8.63k | s->version = version; |
2194 | 8.63k | if (!ssl_set_record_protocol_version(s, version)) { |
2195 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2196 | 0 | return 0; |
2197 | 0 | } |
2198 | | |
2199 | 8.63k | return 1; |
2200 | 8.63k | } |
2201 | | |
2202 | | int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt, |
2203 | | unsigned int context, X509 *x, |
2204 | | size_t chainidx) |
2205 | 8.71k | { |
2206 | 8.71k | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
2207 | 8.71k | unsigned int group_id; |
2208 | 8.71k | PACKET encoded_pt; |
2209 | 8.71k | EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL; |
2210 | 8.71k | const TLS_GROUP_INFO *ginf = NULL; |
2211 | 8.71k | uint16_t valid_ks_id = 0; |
2212 | 8.71k | size_t i; |
2213 | | |
2214 | | /* Sanity check */ |
2215 | 8.71k | if (ckey == NULL || s->s3.peer_tmp != NULL) { |
2216 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2217 | 0 | return 0; |
2218 | 0 | } |
2219 | | |
2220 | | /* Which group ID does the server want -> group_id */ |
2221 | 8.71k | if (!PACKET_get_net_2(pkt, &group_id)) { |
2222 | 1 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2223 | 1 | return 0; |
2224 | 1 | } |
2225 | | |
2226 | | /* RFC 8701: reject GREASE values selected by the server */ |
2227 | 8.71k | if (ossl_is_grease_value(group_id)) { |
2228 | 5 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2229 | 5 | return 0; |
2230 | 5 | } |
2231 | | |
2232 | 8.71k | if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) { |
2233 | 158 | const uint16_t *pgroups = NULL; |
2234 | 158 | size_t num_groups; |
2235 | 158 | const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION |
2236 | 158 | : TLS1_3_VERSION; |
2237 | | |
2238 | 158 | if (PACKET_remaining(pkt) != 0) { |
2239 | 7 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2240 | 7 | return 0; |
2241 | 7 | } |
2242 | | |
2243 | | /* |
2244 | | * It is an error if the HelloRetryRequest wants a key_share that we |
2245 | | * already sent in the first ClientHello |
2246 | | */ |
2247 | 451 | for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { |
2248 | 302 | if (s->s3.tmp.ks_group_id[i] == group_id) { |
2249 | 2 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2250 | 2 | return 0; |
2251 | 2 | } |
2252 | 302 | } |
2253 | | |
2254 | | /* Validate the selected group is one we support */ |
2255 | 149 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
2256 | 1.19k | for (i = 0; i < num_groups; i++) { |
2257 | 1.15k | if (group_id == pgroups[i]) |
2258 | 108 | break; |
2259 | 1.15k | } |
2260 | | |
2261 | 149 | if (i >= num_groups |
2262 | 108 | || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED) |
2263 | 108 | || !tls_valid_group(s, group_id, version1_3, version1_3, NULL, NULL)) { |
2264 | 41 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2265 | 41 | return 0; |
2266 | 41 | } |
2267 | | |
2268 | | /* Memorize which groupID the server wants */ |
2269 | 108 | s->s3.group_id = group_id; |
2270 | | |
2271 | | /* The initial keyshares are obsolete now, hence free memory */ |
2272 | 324 | for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { |
2273 | 216 | if (s->s3.tmp.ks_pkey[i] != NULL) { |
2274 | 216 | EVP_PKEY_free(s->s3.tmp.ks_pkey[i]); |
2275 | 216 | s->s3.tmp.ks_pkey[i] = NULL; |
2276 | 216 | } |
2277 | 216 | } |
2278 | 108 | s->s3.tmp.num_ks_pkey = 0; |
2279 | 108 | s->s3.tmp.pkey = NULL; |
2280 | | |
2281 | 108 | return 1; |
2282 | 149 | } |
2283 | | |
2284 | | /* |
2285 | | * check that the group requested by the server is one we've |
2286 | | * sent a key share for, and if so: memorize which one |
2287 | | */ |
2288 | 17.1k | for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { |
2289 | 17.0k | if (s->s3.tmp.ks_group_id[i] == group_id) { |
2290 | 8.49k | valid_ks_id = group_id; |
2291 | 8.49k | ckey = s->s3.tmp.ks_pkey[i]; |
2292 | 8.49k | s->s3.group_id = group_id; |
2293 | 8.49k | s->s3.tmp.pkey = ckey; |
2294 | 8.49k | break; |
2295 | 8.49k | } |
2296 | 17.0k | } |
2297 | 8.55k | if (valid_ks_id == 0) { |
2298 | | /* |
2299 | | * This isn't for the group that we sent in the original |
2300 | | * key_share! |
2301 | | */ |
2302 | 60 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2303 | 60 | return 0; |
2304 | 60 | } |
2305 | | /* Retain this group in the SSL_SESSION */ |
2306 | 8.49k | if (!s->hit) { |
2307 | 8.49k | s->session->kex_group = group_id; |
2308 | 8.49k | } else if (group_id != s->session->kex_group) { |
2309 | | /* |
2310 | | * If this is a resumption but changed what group was used, we need |
2311 | | * to record the new group in the session, but the session is not |
2312 | | * a new session and could be in use by other threads. So, make |
2313 | | * a copy of the session to record the new information so that it's |
2314 | | * useful for any sessions resumed from tickets issued on this |
2315 | | * connection. |
2316 | | */ |
2317 | 0 | SSL_SESSION *new_sess; |
2318 | |
|
2319 | 0 | if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) { |
2320 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
2321 | 0 | return 0; |
2322 | 0 | } |
2323 | 0 | SSL_SESSION_free(s->session); |
2324 | 0 | s->session = new_sess; |
2325 | 0 | s->session->kex_group = group_id; |
2326 | 0 | } |
2327 | | |
2328 | 8.49k | if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
2329 | 8.49k | group_id)) |
2330 | 8.49k | == NULL) { |
2331 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2332 | 0 | return 0; |
2333 | 0 | } |
2334 | | |
2335 | 8.49k | if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt) |
2336 | 8.44k | || PACKET_remaining(&encoded_pt) == 0) { |
2337 | 46 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2338 | 46 | return 0; |
2339 | 46 | } |
2340 | | |
2341 | 8.44k | if (!ginf->is_kem) { |
2342 | | /* Regular KEX */ |
2343 | 8.41k | skey = EVP_PKEY_new(); |
2344 | 8.41k | if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) { |
2345 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
2346 | 0 | EVP_PKEY_free(skey); |
2347 | 0 | return 0; |
2348 | 0 | } |
2349 | | |
2350 | 8.41k | if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt), |
2351 | 8.41k | PACKET_remaining(&encoded_pt)) |
2352 | 8.41k | <= 0) { |
2353 | 19 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2354 | 19 | EVP_PKEY_free(skey); |
2355 | 19 | return 0; |
2356 | 19 | } |
2357 | | |
2358 | 8.39k | if (ssl_derive(s, ckey, skey, 1) == 0) { |
2359 | | /* SSLfatal() already called */ |
2360 | 6 | EVP_PKEY_free(skey); |
2361 | 6 | return 0; |
2362 | 6 | } |
2363 | 8.39k | s->s3.peer_tmp = skey; |
2364 | 8.39k | } else { |
2365 | | /* KEM Mode */ |
2366 | 31 | const unsigned char *ct = PACKET_data(&encoded_pt); |
2367 | 31 | size_t ctlen = PACKET_remaining(&encoded_pt); |
2368 | | |
2369 | 31 | if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) { |
2370 | | /* SSLfatal() already called */ |
2371 | 4 | return 0; |
2372 | 4 | } |
2373 | 31 | } |
2374 | 8.41k | s->s3.did_kex = 1; |
2375 | 8.41k | #endif |
2376 | | |
2377 | 8.41k | return 1; |
2378 | 8.44k | } |
2379 | | |
2380 | | int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
2381 | | X509 *x, size_t chainidx) |
2382 | 317 | { |
2383 | 317 | PACKET cookie; |
2384 | | |
2385 | 317 | if (!PACKET_as_length_prefixed_2(pkt, &cookie) |
2386 | 296 | || PACKET_remaining(&cookie) == 0 |
2387 | 294 | || !PACKET_memdup(&cookie, &s->ext.tls13_cookie, |
2388 | 294 | &s->ext.tls13_cookie_len)) { |
2389 | 23 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2390 | 23 | return 0; |
2391 | 23 | } |
2392 | | |
2393 | 294 | return 1; |
2394 | 317 | } |
2395 | | |
2396 | | int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt, |
2397 | | unsigned int context, |
2398 | | X509 *x, size_t chainidx) |
2399 | 0 | { |
2400 | 0 | if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { |
2401 | 0 | unsigned long max_early_data; |
2402 | |
|
2403 | 0 | if (!PACKET_get_net_4(pkt, &max_early_data) |
2404 | 0 | || PACKET_remaining(pkt) != 0) { |
2405 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA); |
2406 | 0 | return 0; |
2407 | 0 | } |
2408 | | |
2409 | 0 | s->session->ext.max_early_data = max_early_data; |
2410 | |
|
2411 | 0 | if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) { |
2412 | | /* |
2413 | | * QUIC allows missing max_early_data, or a max_early_data value |
2414 | | * of 0xffffffff. Missing max_early_data is stored in the session |
2415 | | * as 0. This is indistinguishable in OpenSSL from a present |
2416 | | * max_early_data value that was 0. In order that later checks for |
2417 | | * invalid max_early_data correctly treat as an error the case where |
2418 | | * max_early_data is present and it is 0, we store any invalid |
2419 | | * value in the same (non-zero) way. Otherwise we would have to |
2420 | | * introduce a new flag just for this. |
2421 | | */ |
2422 | 0 | s->session->ext.max_early_data = 1; |
2423 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA); |
2424 | 0 | return 0; |
2425 | 0 | } |
2426 | | |
2427 | 0 | return 1; |
2428 | 0 | } |
2429 | | |
2430 | 0 | if (PACKET_remaining(pkt) != 0) { |
2431 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2432 | 0 | return 0; |
2433 | 0 | } |
2434 | | |
2435 | 0 | if (!s->ext.early_data_ok |
2436 | 0 | || !s->hit) { |
2437 | | /* |
2438 | | * If we get here then we didn't send early data, or we didn't resume |
2439 | | * using the first identity, or the SNI/ALPN is not consistent so the |
2440 | | * server should not be accepting it. |
2441 | | */ |
2442 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
2443 | 0 | return 0; |
2444 | 0 | } |
2445 | | |
2446 | 0 | s->ext.early_data = SSL_EARLY_DATA_ACCEPTED; |
2447 | |
|
2448 | 0 | return 1; |
2449 | 0 | } |
2450 | | |
2451 | | int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt, |
2452 | | unsigned int context, X509 *x, |
2453 | | size_t chainidx) |
2454 | 0 | { |
2455 | 0 | #if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3)) |
2456 | 0 | SSL_SESSION *sesstmp; |
2457 | 0 | unsigned int identity; |
2458 | |
|
2459 | 0 | if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) { |
2460 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2461 | 0 | return 0; |
2462 | 0 | } |
2463 | | |
2464 | 0 | if (identity >= (unsigned int)s->ext.tick_identity) { |
2465 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY); |
2466 | 0 | return 0; |
2467 | 0 | } |
2468 | | |
2469 | | /* |
2470 | | * Session resumption tickets are always sent before PSK tickets. If the |
2471 | | * ticket index is 0 then it must be for a session resumption ticket if we |
2472 | | * sent two tickets, or if we didn't send a PSK ticket. |
2473 | | */ |
2474 | 0 | if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) { |
2475 | 0 | s->hit = 1; |
2476 | 0 | SSL_SESSION_free(s->psksession); |
2477 | 0 | s->psksession = NULL; |
2478 | 0 | return 1; |
2479 | 0 | } |
2480 | | |
2481 | 0 | if (s->psksession == NULL) { |
2482 | | /* Should never happen */ |
2483 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2484 | 0 | return 0; |
2485 | 0 | } |
2486 | | |
2487 | | /* |
2488 | | * If we used the external PSK for sending early_data then s->early_secret |
2489 | | * is already set up, so don't overwrite it. Otherwise we copy the |
2490 | | * early_secret across that we generated earlier. |
2491 | | */ |
2492 | 0 | if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY |
2493 | 0 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) |
2494 | 0 | || s->ext.early_data_session != s->psksession) |
2495 | 0 | memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE); |
2496 | | |
2497 | | /* |
2498 | | * The psk_use_session_cb()/psk_client_callback() may reuse |
2499 | | * the session across connections we can't mutate it directly. |
2500 | | */ |
2501 | 0 | if ((sesstmp = ssl_session_dup(s->psksession, 0)) == NULL) { |
2502 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2503 | 0 | return 0; |
2504 | 0 | } |
2505 | 0 | SSL_SESSION_free(s->psksession); |
2506 | 0 | s->psksession = sesstmp; |
2507 | | |
2508 | | /* |
2509 | | * s->psksession (now our private copy) was built by the callback, not via |
2510 | | * ssl_get_new_session(), so it was never stamped with our own sid_ctx. Do |
2511 | | * so now, to avoid rejection of the PSK session in tls_process_server_hello(). |
2512 | | */ |
2513 | 0 | memcpy(s->psksession->sid_ctx, s->sid_ctx, s->sid_ctx_length); |
2514 | 0 | s->psksession->sid_ctx_length = s->sid_ctx_length; |
2515 | |
|
2516 | 0 | SSL_SESSION_free(s->session); |
2517 | 0 | s->session = s->psksession; |
2518 | 0 | s->psksession = NULL; |
2519 | 0 | s->hit = 1; |
2520 | | /* Early data is only allowed if we used the first ticket */ |
2521 | 0 | if (identity != 0) |
2522 | 0 | s->ext.early_data_ok = 0; |
2523 | 0 | #endif |
2524 | |
|
2525 | 0 | return 1; |
2526 | 0 | } |
2527 | | |
2528 | | EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt, |
2529 | | unsigned int context, |
2530 | | X509 *x, size_t chainidx) |
2531 | 60.8k | { |
2532 | 60.8k | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2533 | 60.8k | if (sc->client_cert_type == NULL) |
2534 | 60.8k | return EXT_RETURN_NOT_SENT; |
2535 | 0 | #ifndef OPENSSL_NO_ECH |
2536 | 0 | ECH_SAME_EXT(sc, context, pkt) |
2537 | 0 | #endif |
2538 | | |
2539 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type) |
2540 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2541 | 0 | || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len) |
2542 | 0 | || !WPACKET_close(pkt)) { |
2543 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2544 | 0 | return EXT_RETURN_FAIL; |
2545 | 0 | } |
2546 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD; |
2547 | 0 | return EXT_RETURN_SENT; |
2548 | 0 | } |
2549 | | |
2550 | | int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt, |
2551 | | unsigned int context, |
2552 | | X509 *x, size_t chainidx) |
2553 | 0 | { |
2554 | 0 | unsigned int type; |
2555 | |
|
2556 | 0 | if (PACKET_remaining(pkt) != 1) { |
2557 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2558 | 0 | return 0; |
2559 | 0 | } |
2560 | 0 | if (!PACKET_get_1(pkt, &type)) { |
2561 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2562 | 0 | return 0; |
2563 | 0 | } |
2564 | | /* We did not send/ask for this */ |
2565 | 0 | if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) { |
2566 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2567 | 0 | return 0; |
2568 | 0 | } |
2569 | | /* We don't have this enabled */ |
2570 | 0 | if (sc->client_cert_type == NULL) { |
2571 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2572 | 0 | return 0; |
2573 | 0 | } |
2574 | | /* Given back a value we didn't configure */ |
2575 | 0 | if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) { |
2576 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE); |
2577 | 0 | return 0; |
2578 | 0 | } |
2579 | 0 | sc->ext.client_cert_type = type; |
2580 | 0 | return 1; |
2581 | 0 | } |
2582 | | |
2583 | | EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt, |
2584 | | unsigned int context, |
2585 | | X509 *x, size_t chainidx) |
2586 | 60.8k | { |
2587 | 60.8k | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2588 | 60.8k | if (sc->server_cert_type == NULL) |
2589 | 60.8k | return EXT_RETURN_NOT_SENT; |
2590 | 0 | #ifndef OPENSSL_NO_ECH |
2591 | 0 | ECH_SAME_EXT(sc, context, pkt) |
2592 | 0 | #endif |
2593 | | |
2594 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type) |
2595 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2596 | 0 | || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len) |
2597 | 0 | || !WPACKET_close(pkt)) { |
2598 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2599 | 0 | return EXT_RETURN_FAIL; |
2600 | 0 | } |
2601 | 0 | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD; |
2602 | 0 | return EXT_RETURN_SENT; |
2603 | 0 | } |
2604 | | |
2605 | | int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt, |
2606 | | unsigned int context, |
2607 | | X509 *x, size_t chainidx) |
2608 | 0 | { |
2609 | 0 | unsigned int type; |
2610 | |
|
2611 | 0 | if (PACKET_remaining(pkt) != 1) { |
2612 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2613 | 0 | return 0; |
2614 | 0 | } |
2615 | 0 | if (!PACKET_get_1(pkt, &type)) { |
2616 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2617 | 0 | return 0; |
2618 | 0 | } |
2619 | | /* We did not send/ask for this */ |
2620 | 0 | if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) { |
2621 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2622 | 0 | return 0; |
2623 | 0 | } |
2624 | | /* We don't have this enabled */ |
2625 | 0 | if (sc->server_cert_type == NULL) { |
2626 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2627 | 0 | return 0; |
2628 | 0 | } |
2629 | | /* Given back a value we didn't configure */ |
2630 | 0 | if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) { |
2631 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE); |
2632 | 0 | return 0; |
2633 | 0 | } |
2634 | 0 | sc->ext.server_cert_type = type; |
2635 | 0 | return 1; |
2636 | 0 | } |
2637 | | |
2638 | | #ifndef OPENSSL_NO_ECH |
2639 | | EXT_RETURN tls_construct_ctos_ech(SSL_CONNECTION *s, WPACKET *pkt, |
2640 | | unsigned int context, X509 *x, |
2641 | | size_t chainidx) |
2642 | 56.4k | { |
2643 | 56.4k | int rv = 0, hpke_mode = OSSL_HPKE_MODE_BASE; |
2644 | 56.4k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2645 | 56.4k | OSSL_ECHSTORE_ENTRY *ee = NULL; |
2646 | 56.4k | OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT; |
2647 | 56.4k | unsigned char config_id_to_use = 0x00, info[OSSL_ECH_MAX_INFO_LEN]; |
2648 | 56.4k | unsigned char *encoded = NULL, *mypub = NULL; |
2649 | 56.4k | size_t cipherlen = 0, aad_len = 0, lenclen = 0, mypub_len = 0; |
2650 | 56.4k | size_t info_len = OSSL_ECH_MAX_INFO_LEN, clear_len = 0, encoded_len = 0; |
2651 | | /* whether or not we've been asked to GREASE, one way or another */ |
2652 | 56.4k | int grease_opt_set = (s->ext.ech.attempted != 1 |
2653 | 56.4k | && ((s->ext.ech.grease == OSSL_ECH_IS_GREASE) |
2654 | 56.4k | || ((s->options & SSL_OP_ECH_GREASE) != 0))); |
2655 | | |
2656 | | /* if we're not doing real ECH and not GREASEing then exit */ |
2657 | 56.4k | if (s->ext.ech.attempted_type != TLSEXT_TYPE_ech && grease_opt_set == 0) |
2658 | 56.4k | return EXT_RETURN_NOT_SENT; |
2659 | | /* send grease if not really attempting ECH */ |
2660 | 0 | if (grease_opt_set == 1) { |
2661 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING |
2662 | 0 | && s->ext.ech.sent != NULL) { |
2663 | | /* re-tx already sent GREASEy ECH */ |
2664 | 0 | if (WPACKET_memcpy(pkt, s->ext.ech.sent, |
2665 | 0 | s->ext.ech.sent_len) |
2666 | 0 | != 1) { |
2667 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2668 | 0 | return EXT_RETURN_FAIL; |
2669 | 0 | } |
2670 | 0 | return EXT_RETURN_SENT; |
2671 | 0 | } |
2672 | | /* if nobody set a type, use the default */ |
2673 | 0 | if (s->ext.ech.attempted_type == OSSL_ECH_type_unknown) |
2674 | 0 | s->ext.ech.attempted_type = TLSEXT_TYPE_ech; |
2675 | 0 | if (ossl_ech_send_grease(s, pkt) != 1) { |
2676 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2677 | 0 | return EXT_RETURN_FAIL; |
2678 | 0 | } |
2679 | 0 | return EXT_RETURN_SENT; |
2680 | 0 | } |
2681 | | |
2682 | | /* For the inner CH - we simply include one of these saying "inner" */ |
2683 | 0 | if (s->ext.ech.ch_depth == 1) { |
2684 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech) |
2685 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2686 | 0 | || !WPACKET_put_bytes_u8(pkt, OSSL_ECH_INNER_CH_TYPE) |
2687 | 0 | || !WPACKET_close(pkt)) { |
2688 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2689 | 0 | return EXT_RETURN_FAIL; |
2690 | 0 | } |
2691 | 0 | return EXT_RETURN_SENT; |
2692 | 0 | } |
2693 | | |
2694 | | /* |
2695 | | * If not GREASEing we prepare sending the outer value - after the |
2696 | | * entire thing has been constructed, putting in zeros for now where |
2697 | | * we'd otherwise include ECH ciphertext, we later encode and encrypt. |
2698 | | * We need to do it that way as we need the rest of the outer CH to |
2699 | | * be known and used as AAD input before we do encryption. |
2700 | | */ |
2701 | 0 | if (s->ext.ech.ch_depth != 0) |
2702 | 0 | return EXT_RETURN_NOT_SENT; |
2703 | | /* Make ClientHelloInner and EncodedClientHelloInner as per spec. */ |
2704 | 0 | if (ossl_ech_encode_inner(s, &encoded, &encoded_len) != 1) { |
2705 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2706 | 0 | goto err; |
2707 | 0 | } |
2708 | 0 | s->ext.ech.encoded_inner = encoded; |
2709 | 0 | s->ext.ech.encoded_inner_len = encoded_len; |
2710 | 0 | #ifdef OSSL_ECH_SUPERVERBOSE |
2711 | 0 | ossl_ech_pbuf("encoded inner CH", encoded, encoded_len); |
2712 | 0 | #endif |
2713 | 0 | rv = ossl_ech_pick_matching_cfg(s, &ee, &hpke_suite); |
2714 | 0 | if (rv != 1 || ee == NULL) { |
2715 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2716 | 0 | goto err; |
2717 | 0 | } |
2718 | 0 | s->ext.ech.attempted_type = ee->version; |
2719 | 0 | OSSL_TRACE_BEGIN(TLS) |
2720 | 0 | { |
2721 | 0 | BIO_printf(trc_out, "EAAE: selected: version: %4x, config %2x\n", |
2722 | 0 | ee->version, ee->config_id); |
2723 | 0 | } |
2724 | 0 | OSSL_TRACE_END(TLS); |
2725 | 0 | config_id_to_use = ee->config_id; /* if requested, use a random config_id instead */ |
2726 | 0 | if ((s->options & SSL_OP_ECH_IGNORE_CID) != 0) { |
2727 | 0 | int max_iters = 1000, i = 0; |
2728 | | |
2729 | | /* rejection sample to get a different but random config_id */ |
2730 | 0 | while (config_id_to_use == ee->config_id) { |
2731 | 0 | #ifdef OSSL_ECH_SUPERVERBOSE |
2732 | 0 | if (i > 0) { |
2733 | 0 | OSSL_TRACE_BEGIN(TLS) |
2734 | 0 | { |
2735 | 0 | BIO_printf(trc_out, "EAAE: rejected random-config %02x\n", |
2736 | 0 | config_id_to_use); |
2737 | 0 | } |
2738 | 0 | OSSL_TRACE_END(TLS); |
2739 | 0 | } |
2740 | 0 | #endif |
2741 | 0 | if (RAND_bytes_ex(sctx->libctx, &config_id_to_use, 1, 0) <= 0) { |
2742 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2743 | 0 | return 0; |
2744 | 0 | } |
2745 | 0 | if (i++ >= max_iters) { |
2746 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2747 | 0 | return 0; |
2748 | 0 | } |
2749 | 0 | } |
2750 | 0 | #ifdef OSSL_ECH_SUPERVERBOSE |
2751 | 0 | ossl_ech_pbuf("EAAE: random config_id", &config_id_to_use, 1); |
2752 | 0 | #endif |
2753 | 0 | } |
2754 | 0 | s->ext.ech.attempted_cid = config_id_to_use; |
2755 | 0 | #ifdef OSSL_ECH_SUPERVERBOSE |
2756 | 0 | ossl_ech_pbuf("EAAE: peer pub", ee->pub, ee->pub_len); |
2757 | 0 | ossl_ech_pbuf("EAAE: clear", encoded, encoded_len); |
2758 | 0 | ossl_ech_pbuf("EAAE: ECHConfig", ee->encoded, ee->encoded_len); |
2759 | 0 | #endif |
2760 | | /* |
2761 | | * The AAD is the full outer client hello but with the correct number of |
2762 | | * zeros for where the ECH ciphertext octets will later be placed. So we |
2763 | | * add the ECH extension to the |pkt| but with zeros for ciphertext, that |
2764 | | * forms up the AAD, then after we've encrypted, we'll splice in the actual |
2765 | | * ciphertext. |
2766 | | * Watch out for the "4" offsets that remove the type and 3-octet length |
2767 | | * from the encoded CH as per the spec. |
2768 | | */ |
2769 | 0 | clear_len = ossl_ech_calc_padding(s, ee, encoded_len); |
2770 | 0 | if (clear_len == 0) { |
2771 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2772 | 0 | goto err; |
2773 | 0 | } |
2774 | 0 | lenclen = OSSL_HPKE_get_public_encap_size(hpke_suite); |
2775 | 0 | if (s->ext.ech.hpke_ctx == NULL) { /* 1st CH */ |
2776 | 0 | if (ossl_ech_make_enc_info(ee->encoded, ee->encoded_len, |
2777 | 0 | info, &info_len) |
2778 | 0 | != 1) { |
2779 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2780 | 0 | goto err; |
2781 | 0 | } |
2782 | 0 | #ifdef OSSL_ECH_SUPERVERBOSE |
2783 | 0 | ossl_ech_pbuf("EAAE info", info, info_len); |
2784 | 0 | #endif |
2785 | 0 | s->ext.ech.hpke_ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite, |
2786 | 0 | OSSL_HPKE_ROLE_SENDER, |
2787 | 0 | sctx->libctx, sctx->propq); |
2788 | 0 | if (s->ext.ech.hpke_ctx == NULL) { |
2789 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2790 | 0 | goto err; |
2791 | 0 | } |
2792 | 0 | mypub = OPENSSL_malloc(lenclen); |
2793 | 0 | if (mypub == NULL) { |
2794 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2795 | 0 | goto err; |
2796 | 0 | } |
2797 | 0 | mypub_len = lenclen; |
2798 | 0 | rv = OSSL_HPKE_encap(s->ext.ech.hpke_ctx, mypub, &mypub_len, |
2799 | 0 | ee->pub, ee->pub_len, info, info_len); |
2800 | 0 | if (rv != 1) { |
2801 | 0 | OPENSSL_free(mypub); |
2802 | 0 | mypub = NULL; |
2803 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2804 | 0 | goto err; |
2805 | 0 | } |
2806 | 0 | s->ext.ech.pub = mypub; |
2807 | 0 | s->ext.ech.pub_len = mypub_len; |
2808 | 0 | } else { /* HRR - retrieve public */ |
2809 | 0 | mypub = s->ext.ech.pub; |
2810 | 0 | mypub_len = s->ext.ech.pub_len; |
2811 | 0 | if (mypub == NULL || mypub_len == 0) { |
2812 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2813 | 0 | goto err; |
2814 | 0 | } |
2815 | 0 | } |
2816 | 0 | #ifdef OSSL_ECH_SUPERVERBOSE |
2817 | 0 | ossl_ech_pbuf("EAAE: mypub", mypub, mypub_len); |
2818 | 0 | WPACKET_get_total_written(pkt, &aad_len); /* use aad_len for tracing */ |
2819 | 0 | ossl_ech_pbuf("EAAE pkt b4", WPACKET_get_curr(pkt) - aad_len, aad_len); |
2820 | 0 | #endif |
2821 | 0 | cipherlen = OSSL_HPKE_get_ciphertext_size(hpke_suite, clear_len); |
2822 | 0 | if (cipherlen <= clear_len || cipherlen > OSSL_ECH_MAX_PAYLOAD_LEN) { |
2823 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2824 | 0 | goto err; |
2825 | 0 | } |
2826 | 0 | s->ext.ech.clearlen = clear_len; |
2827 | 0 | s->ext.ech.cipherlen = cipherlen; |
2828 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech) |
2829 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2830 | 0 | || !WPACKET_put_bytes_u8(pkt, OSSL_ECH_OUTER_CH_TYPE) |
2831 | 0 | || !WPACKET_put_bytes_u16(pkt, hpke_suite.kdf_id) |
2832 | 0 | || !WPACKET_put_bytes_u16(pkt, hpke_suite.aead_id) |
2833 | 0 | || !WPACKET_put_bytes_u8(pkt, config_id_to_use) |
2834 | 0 | || (s->hello_retry_request == SSL_HRR_PENDING |
2835 | 0 | && !WPACKET_put_bytes_u16(pkt, 0x00)) /* no pub */ |
2836 | 0 | || (s->hello_retry_request != SSL_HRR_PENDING |
2837 | 0 | && !WPACKET_sub_memcpy_u16(pkt, mypub, mypub_len)) |
2838 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2839 | 0 | || !WPACKET_get_total_written(pkt, &s->ext.ech.cipher_offset) |
2840 | 0 | || !WPACKET_memset(pkt, 0, cipherlen) |
2841 | 0 | || !WPACKET_close(pkt) |
2842 | 0 | || !WPACKET_close(pkt)) { |
2843 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2844 | 0 | goto err; |
2845 | 0 | } |
2846 | | /* don't count the type + 3-octet length */ |
2847 | 0 | s->ext.ech.cipher_offset -= 4; |
2848 | 0 | return EXT_RETURN_SENT; |
2849 | 0 | err: |
2850 | 0 | return EXT_RETURN_FAIL; |
2851 | 0 | } |
2852 | | |
2853 | | /* if the server thinks we GREASE'd then we may get an ECHConfigList */ |
2854 | | int tls_parse_stoc_ech(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
2855 | | X509 *x, size_t chainidx) |
2856 | 0 | { |
2857 | 0 | size_t rlen = 0; |
2858 | 0 | const unsigned char *rval = NULL; |
2859 | 0 | unsigned char *srval = NULL; |
2860 | 0 | PACKET rcfgs_pkt; |
2861 | | |
2862 | | /* |
2863 | | * An HRR will have an ECH extension with the 8-octet confirmation value. |
2864 | | * Store it away for when we check it later |
2865 | | */ |
2866 | 0 | if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) { |
2867 | 0 | if (PACKET_remaining(pkt) != OSSL_ECH_SIGNAL_LEN) { |
2868 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2869 | 0 | return 0; |
2870 | 0 | } |
2871 | 0 | s->ext.ech.hrrsignal_p = (unsigned char *)PACKET_data(pkt); |
2872 | 0 | memcpy(s->ext.ech.hrrsignal, s->ext.ech.hrrsignal_p, |
2873 | 0 | OSSL_ECH_SIGNAL_LEN); |
2874 | 0 | return 1; |
2875 | 0 | } |
2876 | | /* otherwise we expect retry-configs */ |
2877 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &rcfgs_pkt)) { |
2878 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2879 | 0 | return 0; |
2880 | 0 | } |
2881 | 0 | rval = PACKET_data(&rcfgs_pkt); |
2882 | 0 | rlen = (unsigned int)PACKET_remaining(&rcfgs_pkt); |
2883 | 0 | OPENSSL_free(s->ext.ech.returned); |
2884 | 0 | s->ext.ech.returned = NULL; |
2885 | 0 | srval = OPENSSL_malloc(rlen + 2); |
2886 | 0 | if (srval == NULL) { |
2887 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2888 | 0 | return 0; |
2889 | 0 | } |
2890 | 0 | srval[0] = (rlen >> 8) & 0xff; |
2891 | 0 | srval[1] = rlen & 0xff; |
2892 | 0 | memcpy(srval + 2, rval, rlen); |
2893 | 0 | s->ext.ech.returned = srval; |
2894 | 0 | s->ext.ech.returned_len = rlen + 2; |
2895 | 0 | return 1; |
2896 | 0 | } |
2897 | | #endif /* END_OPENSSL_NO_ECH */ |
2898 | | |
2899 | | /* |
2900 | | * RFC 8701 GREASE extension constructors. Each writes an empty extension |
2901 | | * whose type is a GREASE value (0x?A?A pattern). |
2902 | | */ |
2903 | | EXT_RETURN tls_construct_ctos_grease1(SSL_CONNECTION *s, WPACKET *pkt, |
2904 | | unsigned int context, X509 *x, |
2905 | | size_t chainidx) |
2906 | 42.9k | { |
2907 | 42.9k | uint16_t grease_type; |
2908 | | |
2909 | 42.9k | if (!(s->options & SSL_OP_GREASE) || s->server) |
2910 | 42.9k | return EXT_RETURN_NOT_SENT; |
2911 | | |
2912 | 0 | grease_type = ossl_grease_value(s, OSSL_GREASE_EXT1); |
2913 | |
|
2914 | 0 | if (!WPACKET_put_bytes_u16(pkt, grease_type) |
2915 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
2916 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2917 | 0 | return EXT_RETURN_FAIL; |
2918 | 0 | } |
2919 | | |
2920 | 0 | return EXT_RETURN_SENT; |
2921 | 0 | } |
2922 | | |
2923 | | EXT_RETURN tls_construct_ctos_grease2(SSL_CONNECTION *s, WPACKET *pkt, |
2924 | | unsigned int context, X509 *x, |
2925 | | size_t chainidx) |
2926 | 42.9k | { |
2927 | 42.9k | uint16_t grease_type; |
2928 | | |
2929 | 42.9k | if (!(s->options & SSL_OP_GREASE) || s->server) |
2930 | 42.9k | return EXT_RETURN_NOT_SENT; |
2931 | | |
2932 | 0 | grease_type = ossl_grease_value(s, OSSL_GREASE_EXT2); |
2933 | | |
2934 | | /* |
2935 | | * RFC 8701 recommends "varying length and contents" for GREASE |
2936 | | * extensions. Extension 1 is empty; extension 2 carries one zero byte |
2937 | | * so that servers are tested against both empty and non-empty unknown |
2938 | | * extensions. This mirrors the BoringSSL behaviour. |
2939 | | */ |
2940 | 0 | if (!WPACKET_put_bytes_u16(pkt, grease_type) |
2941 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2942 | 0 | || !WPACKET_put_bytes_u8(pkt, 0) |
2943 | 0 | || !WPACKET_close(pkt)) { |
2944 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2945 | 0 | return EXT_RETURN_FAIL; |
2946 | 0 | } |
2947 | | |
2948 | 0 | return EXT_RETURN_SENT; |
2949 | 0 | } |