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