/src/openssl/ssl/statem/extensions_clnt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/ocsp.h> |
11 | | #include "../ssl_local.h" |
12 | | #include "internal/cryptlib.h" |
13 | | #include "internal/ssl_unwrap.h" |
14 | | #include "internal/tlsgroups.h" |
15 | | #include "statem_local.h" |
16 | | |
17 | | /* Used in the negotiate_dhe function */ |
18 | | typedef enum { |
19 | | ffdhe_check, |
20 | | ecdhe_check, |
21 | | ptfmt_check |
22 | | } dhe_check_t; |
23 | | |
24 | | EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt, |
25 | | unsigned int context, X509 *x, |
26 | | size_t chainidx) |
27 | 89.5k | { |
28 | 89.5k | if (!s->renegotiate) { |
29 | | /* If not renegotiating, send an empty RI extension to indicate support */ |
30 | | |
31 | | #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION |
32 | | #error Internal DTLS version error |
33 | | #endif |
34 | | |
35 | 88.4k | if (!SSL_CONNECTION_IS_DTLS(s) |
36 | 67.1k | && (s->min_proto_version >= TLS1_3_VERSION |
37 | 26.5k | || (ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL) |
38 | 67.1k | && s->min_proto_version <= TLS1_VERSION))) { |
39 | | /* |
40 | | * For TLS <= 1.0 SCSV is used instead, and for TLS 1.3 this |
41 | | * extension isn't used at all. |
42 | | */ |
43 | 67.1k | return EXT_RETURN_NOT_SENT; |
44 | 67.1k | } |
45 | | |
46 | 21.3k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
47 | 21.3k | || !WPACKET_start_sub_packet_u16(pkt) |
48 | 21.3k | || !WPACKET_put_bytes_u8(pkt, 0) |
49 | 21.3k | || !WPACKET_close(pkt)) { |
50 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
51 | 0 | return EXT_RETURN_FAIL; |
52 | 0 | } |
53 | | |
54 | 21.3k | return EXT_RETURN_SENT; |
55 | 21.3k | } |
56 | | |
57 | | /* Add a complete RI extension if renegotiating */ |
58 | 1.06k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
59 | 1.06k | || !WPACKET_start_sub_packet_u16(pkt) |
60 | 1.06k | || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished, |
61 | 1.06k | s->s3.previous_client_finished_len) |
62 | 1.06k | || !WPACKET_close(pkt)) { |
63 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
64 | 0 | return EXT_RETURN_FAIL; |
65 | 0 | } |
66 | | |
67 | 1.06k | return EXT_RETURN_SENT; |
68 | 1.06k | } |
69 | | |
70 | | EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt, |
71 | | unsigned int context, X509 *x, |
72 | | size_t chainidx) |
73 | 119k | { |
74 | 119k | if (s->ext.hostname == NULL) |
75 | 0 | return EXT_RETURN_NOT_SENT; |
76 | | |
77 | | /* Add TLS extension servername to the Client Hello message */ |
78 | 119k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) |
79 | | /* Sub-packet for server_name extension */ |
80 | 119k | || !WPACKET_start_sub_packet_u16(pkt) |
81 | | /* Sub-packet for servername list (always 1 hostname)*/ |
82 | 119k | || !WPACKET_start_sub_packet_u16(pkt) |
83 | 119k | || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name) |
84 | 119k | || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname, |
85 | 119k | strlen(s->ext.hostname)) |
86 | 119k | || !WPACKET_close(pkt) |
87 | 119k | || !WPACKET_close(pkt)) { |
88 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
89 | 0 | return EXT_RETURN_FAIL; |
90 | 0 | } |
91 | | |
92 | 119k | return EXT_RETURN_SENT; |
93 | 119k | } |
94 | | |
95 | | /* Push a Max Fragment Len extension into ClientHello */ |
96 | | EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt, |
97 | | unsigned int context, X509 *x, |
98 | | size_t chainidx) |
99 | 119k | { |
100 | 119k | if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED) |
101 | 119k | return EXT_RETURN_NOT_SENT; |
102 | | |
103 | | /* Add Max Fragment Length extension if client enabled it. */ |
104 | | /*- |
105 | | * 4 bytes for this extension type and extension length |
106 | | * 1 byte for the Max Fragment Length code value. |
107 | | */ |
108 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length) |
109 | | /* Sub-packet for Max Fragment Length extension (1 byte) */ |
110 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
111 | 0 | || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode) |
112 | 0 | || !WPACKET_close(pkt)) { |
113 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
114 | 0 | return EXT_RETURN_FAIL; |
115 | 0 | } |
116 | | |
117 | 0 | return EXT_RETURN_SENT; |
118 | 0 | } |
119 | | |
120 | | #ifndef OPENSSL_NO_SRP |
121 | | EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt, |
122 | | unsigned int context, |
123 | | X509 *x, size_t chainidx) |
124 | 119k | { |
125 | | /* Add SRP username if there is one */ |
126 | 119k | if (s->srp_ctx.login == NULL) |
127 | 119k | return EXT_RETURN_NOT_SENT; |
128 | | |
129 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp) |
130 | | /* Sub-packet for SRP extension */ |
131 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
132 | 0 | || !WPACKET_start_sub_packet_u8(pkt) |
133 | | /* login must not be zero...internal error if so */ |
134 | 0 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
135 | 0 | || !WPACKET_memcpy(pkt, s->srp_ctx.login, |
136 | 0 | strlen(s->srp_ctx.login)) |
137 | 0 | || !WPACKET_close(pkt) |
138 | 0 | || !WPACKET_close(pkt)) { |
139 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
140 | 0 | return EXT_RETURN_FAIL; |
141 | 0 | } |
142 | | |
143 | 0 | return EXT_RETURN_SENT; |
144 | 0 | } |
145 | | #endif |
146 | | |
147 | | /* |
148 | | * With (D)TLS < 1.3 the only negotiated supported key exchange groups are |
149 | | * FFDHE (RFC7919) and ECDHE/ECX (RFC8422 + legacy). With (D)TLS 1.3, we add |
150 | | * KEMs, and the supported groups are no longer cipher-dependent. |
151 | | * |
152 | | * This function serves two purposes: |
153 | | * |
154 | | * - To determine whether to send the supported point formats extension. |
155 | | * This is no longer applicable with (D)TLS >= 1.3. |
156 | | * - To determine whether to send the supported groups extension. |
157 | | * |
158 | | * In the former case, we only care about whether both ECC ciphers and EC/ECX |
159 | | * supported groups are configured, and the (D)TLS min version is at most 1.2. |
160 | | * |
161 | | * In the latter case, we also admit DHE ciphers with FFDHE groups, or any TLS |
162 | | * 1.3 cipher, since the extension is effectively mandatory for (D)TLS 1.3, |
163 | | * with the sole exception of psk-ke resumption, provided the client is sure |
164 | | * that the server will not want elect a full handshake. The check type then |
165 | | * indicates whether ECDHE or FFDHE negotiation should be performed. |
166 | | */ |
167 | | static int negotiate_dhe(SSL_CONNECTION *s, dhe_check_t check_type, |
168 | | int min_version, int max_version) |
169 | 60.9k | { |
170 | 60.9k | int i, end, ret = 0; |
171 | 60.9k | STACK_OF(SSL_CIPHER) *cipher_stack = NULL; |
172 | 60.9k | const uint16_t *pgroups = NULL; |
173 | 60.9k | size_t num_groups, j; |
174 | 60.9k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
175 | 60.9k | int dtls = SSL_CONNECTION_IS_DTLS(s); |
176 | | |
177 | | /* See if we support any EC or FFDHE ciphersuites */ |
178 | 60.9k | cipher_stack = SSL_get1_supported_ciphers(ssl); |
179 | 60.9k | end = sk_SSL_CIPHER_num(cipher_stack); |
180 | 117k | for (i = 0; i < end; i++) { |
181 | 107k | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
182 | 107k | unsigned long alg_k = c->algorithm_mkey; |
183 | 107k | unsigned long alg_a = c->algorithm_auth; |
184 | | |
185 | 107k | int is_ffdhe_ciphersuite = (alg_k & (SSL_kDHE | SSL_kDHEPSK)); |
186 | 107k | int is_ec_ciphersuite = ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) |
187 | 83.0k | || (alg_a & SSL_aECDSA)); |
188 | 107k | int is_tls13 = (dtls ? DTLS_VERSION_GT(c->min_dtls, DTLS1_2_VERSION) |
189 | 107k | : (c->min_tls > TLS1_2_VERSION)); |
190 | | |
191 | 107k | if ((check_type == ffdhe_check && (is_ffdhe_ciphersuite || is_tls13)) |
192 | 87.4k | || (check_type == ecdhe_check && (is_ec_ciphersuite || is_tls13)) |
193 | 67.1k | || (check_type == ptfmt_check && is_ec_ciphersuite)) { |
194 | 51.4k | ret = 1; |
195 | 51.4k | break; |
196 | 51.4k | } |
197 | 107k | } |
198 | 60.9k | sk_SSL_CIPHER_free(cipher_stack); |
199 | 60.9k | if (ret == 0) |
200 | 9.52k | return 0; |
201 | | |
202 | | /* Check we have at least one EC or FFDHE supported group */ |
203 | 51.4k | tls1_get_supported_groups(s, &pgroups, &num_groups); |
204 | 214k | for (j = 0; j < num_groups; j++) { |
205 | 210k | uint16_t ctmp = pgroups[j]; |
206 | 210k | const TLS_GROUP_INFO *ginfo = NULL; |
207 | | |
208 | 210k | if (!tls_valid_group(s, ctmp, min_version, max_version, NULL, &ginfo)) |
209 | 24.5k | continue; |
210 | | |
211 | 185k | if (check_type == ffdhe_check && is_ffdhe_group(ginfo->group_id) |
212 | 15.9k | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
213 | 15.9k | return 1; |
214 | | |
215 | 169k | if (check_type != ffdhe_check && is_ecdhe_group(ginfo->group_id) |
216 | 30.9k | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
217 | 30.9k | return 1; |
218 | 169k | } |
219 | 4.60k | return 0; |
220 | 51.4k | } |
221 | | |
222 | | EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt, |
223 | | unsigned int context, X509 *x, |
224 | | size_t chainidx) |
225 | 20.3k | { |
226 | 20.3k | const unsigned char *pformats; |
227 | 20.3k | size_t num_formats; |
228 | 20.3k | int reason, min_version, max_version; |
229 | | |
230 | 20.3k | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
231 | 20.3k | if (reason != 0) { |
232 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
233 | 0 | return EXT_RETURN_FAIL; |
234 | 0 | } |
235 | 20.3k | if (!negotiate_dhe(s, ptfmt_check, min_version, max_version)) |
236 | 9.61k | return EXT_RETURN_NOT_SENT; |
237 | | |
238 | 10.7k | tls1_get_formatlist(s, &pformats, &num_formats); |
239 | 10.7k | if (num_formats == 0) |
240 | 0 | return EXT_RETURN_NOT_SENT; |
241 | | |
242 | | /* Add TLS extension ECPointFormats to the ClientHello message */ |
243 | 10.7k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
244 | | /* Sub-packet for formats extension */ |
245 | 10.7k | || !WPACKET_start_sub_packet_u16(pkt) |
246 | 10.7k | || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats) |
247 | 10.7k | || !WPACKET_close(pkt)) { |
248 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
249 | 0 | return EXT_RETURN_FAIL; |
250 | 0 | } |
251 | | |
252 | 10.7k | return EXT_RETURN_SENT; |
253 | 10.7k | } |
254 | | |
255 | | EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt, |
256 | | unsigned int context, X509 *x, |
257 | | size_t chainidx) |
258 | 20.3k | { |
259 | 20.3k | const uint16_t *pgroups = NULL; |
260 | 20.3k | size_t num_groups = 0, i, tls13added = 0, added = 0; |
261 | 20.3k | int min_version, max_version, reason; |
262 | 20.3k | int dtls = SSL_CONNECTION_IS_DTLS(s); |
263 | 20.3k | int use_ecdhe, use_ffdhe; |
264 | | |
265 | 20.3k | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
266 | 20.3k | if (reason != 0) { |
267 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
268 | 0 | return EXT_RETURN_FAIL; |
269 | 0 | } |
270 | | |
271 | | /* |
272 | | * If we don't support suitable groups, don't send the extension |
273 | | */ |
274 | 20.3k | use_ecdhe = negotiate_dhe(s, ecdhe_check, min_version, max_version); |
275 | 20.3k | use_ffdhe = negotiate_dhe(s, ffdhe_check, min_version, max_version); |
276 | 20.3k | if (!use_ecdhe && !use_ffdhe |
277 | 90 | && (dtls ? DTLS_VERSION_LE(max_version, DTLS1_2_VERSION) |
278 | 90 | : (max_version <= TLS1_2_VERSION))) |
279 | 90 | return EXT_RETURN_NOT_SENT; |
280 | | |
281 | | /* |
282 | | * Add TLS extension supported_groups to the ClientHello message |
283 | | */ |
284 | 20.2k | tls1_get_supported_groups(s, &pgroups, &num_groups); |
285 | | |
286 | 20.2k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) |
287 | | /* Sub-packet for supported_groups extension */ |
288 | 20.2k | || !WPACKET_start_sub_packet_u16(pkt) |
289 | 20.2k | || !WPACKET_start_sub_packet_u16(pkt) |
290 | 20.2k | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) { |
291 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
292 | 0 | return EXT_RETURN_FAIL; |
293 | 0 | } |
294 | | /* Copy group ID if supported */ |
295 | 182k | for (i = 0; i < num_groups; i++) { |
296 | 161k | const TLS_GROUP_INFO *ginfo = NULL; |
297 | 161k | uint16_t ctmp = pgroups[i]; |
298 | 161k | int okfortls13; |
299 | | |
300 | 161k | if (!tls_valid_group(s, ctmp, min_version, max_version, &okfortls13, |
301 | 161k | &ginfo) |
302 | 148k | || (!use_ecdhe && is_ecdhe_group(ginfo->group_id)) |
303 | 148k | || (!use_ffdhe && is_ffdhe_group(ginfo->group_id)) |
304 | | /* Note: SSL_SECOP_CURVE_SUPPORTED covers all key exchange groups */ |
305 | 148k | || !tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) |
306 | 13.2k | continue; |
307 | | |
308 | 148k | if (!WPACKET_put_bytes_u16(pkt, ctmp)) { |
309 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
310 | 0 | return EXT_RETURN_FAIL; |
311 | 0 | } |
312 | 148k | if (okfortls13 && max_version == TLS1_3_VERSION) |
313 | 125k | tls13added++; |
314 | 148k | added++; |
315 | 148k | } |
316 | 20.2k | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
317 | 0 | if (added == 0) |
318 | 0 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS, |
319 | 0 | "No groups enabled for max supported SSL/TLS version"); |
320 | 0 | else |
321 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
322 | 0 | return EXT_RETURN_FAIL; |
323 | 0 | } |
324 | | |
325 | 20.2k | if (tls13added == 0 && max_version == TLS1_3_VERSION) { |
326 | 0 | SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS, |
327 | 0 | "No groups enabled for max supported SSL/TLS version"); |
328 | 0 | return EXT_RETURN_FAIL; |
329 | 0 | } |
330 | | |
331 | 20.2k | return EXT_RETURN_SENT; |
332 | 20.2k | } |
333 | | |
334 | | EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt, |
335 | | unsigned int context, X509 *x, |
336 | | size_t chainidx) |
337 | 119k | { |
338 | 119k | size_t ticklen; |
339 | | |
340 | 119k | if (!tls_use_ticket(s)) |
341 | 0 | return EXT_RETURN_NOT_SENT; |
342 | | |
343 | 119k | if (!s->new_session && s->session != NULL |
344 | 119k | && s->session->ext.tick != NULL |
345 | 125 | && s->session->ssl_version != TLS1_3_VERSION) { |
346 | 125 | ticklen = s->session->ext.ticklen; |
347 | 119k | } else if (s->session && s->ext.session_ticket != NULL |
348 | 0 | && s->ext.session_ticket->data != NULL) { |
349 | 0 | ticklen = s->ext.session_ticket->length; |
350 | 0 | s->session->ext.tick = OPENSSL_malloc(ticklen); |
351 | 0 | if (s->session->ext.tick == NULL) { |
352 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
353 | 0 | return EXT_RETURN_FAIL; |
354 | 0 | } |
355 | 0 | memcpy(s->session->ext.tick, |
356 | 0 | s->ext.session_ticket->data, ticklen); |
357 | 0 | s->session->ext.ticklen = ticklen; |
358 | 119k | } else { |
359 | 119k | ticklen = 0; |
360 | 119k | } |
361 | | |
362 | 119k | if (ticklen == 0 && s->ext.session_ticket != NULL && s->ext.session_ticket->data == NULL) |
363 | 0 | return EXT_RETURN_NOT_SENT; |
364 | | |
365 | 119k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
366 | 119k | || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) { |
367 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
368 | 0 | return EXT_RETURN_FAIL; |
369 | 0 | } |
370 | | |
371 | 119k | return EXT_RETURN_SENT; |
372 | 119k | } |
373 | | |
374 | | EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt, |
375 | | unsigned int context, X509 *x, |
376 | | size_t chainidx) |
377 | 66.4k | { |
378 | 66.4k | size_t salglen; |
379 | 66.4k | const uint16_t *salg; |
380 | | |
381 | | /* |
382 | | * This used both in the initial hello and as part of renegotiation, |
383 | | * in the latter case, the client version may be already set and may |
384 | | * be lower than that initially offered in `client_version`. |
385 | | */ |
386 | 66.4k | if (!SSL_CONNECTION_IS_DTLS(s)) { |
387 | 50.6k | if (s->client_version < TLS1_2_VERSION |
388 | 50.6k | || (s->ssl.method->version != TLS_ANY_VERSION |
389 | 780 | && s->version < TLS1_2_VERSION)) |
390 | 636 | return EXT_RETURN_NOT_SENT; |
391 | 50.6k | } else { |
392 | 15.8k | if (DTLS_VERSION_LT(s->client_version, DTLS1_2_VERSION) |
393 | 15.6k | || (s->ssl.method->version != DTLS_ANY_VERSION |
394 | 94 | && DTLS_VERSION_LT(s->version, DTLS1_2_VERSION))) |
395 | 218 | return EXT_RETURN_NOT_SENT; |
396 | 15.8k | } |
397 | | |
398 | 65.6k | salglen = tls12_get_psigalgs(s, 1, &salg); |
399 | 65.6k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) |
400 | | /* Sub-packet for sig-algs extension */ |
401 | 65.6k | || !WPACKET_start_sub_packet_u16(pkt) |
402 | | /* Sub-packet for the actual list */ |
403 | 65.6k | || !WPACKET_start_sub_packet_u16(pkt) |
404 | 65.6k | || !tls12_copy_sigalgs(s, pkt, salg, salglen) |
405 | 65.6k | || !WPACKET_close(pkt) |
406 | 65.6k | || !WPACKET_close(pkt)) { |
407 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
408 | 0 | return EXT_RETURN_FAIL; |
409 | 0 | } |
410 | | |
411 | 65.6k | return EXT_RETURN_SENT; |
412 | 65.6k | } |
413 | | |
414 | | #ifndef OPENSSL_NO_OCSP |
415 | | EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt, |
416 | | unsigned int context, X509 *x, |
417 | | size_t chainidx) |
418 | 119k | { |
419 | 119k | int i; |
420 | | |
421 | | /* This extension isn't defined for client Certificates */ |
422 | 119k | if (x != NULL) |
423 | 0 | return EXT_RETURN_NOT_SENT; |
424 | | |
425 | 119k | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) |
426 | 119k | return EXT_RETURN_NOT_SENT; |
427 | | |
428 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
429 | | /* Sub-packet for status request extension */ |
430 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
431 | 0 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp) |
432 | | /* Sub-packet for the ids */ |
433 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
434 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
435 | 0 | return EXT_RETURN_FAIL; |
436 | 0 | } |
437 | 0 | for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) { |
438 | 0 | unsigned char *idbytes; |
439 | 0 | OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i); |
440 | 0 | int idlen = i2d_OCSP_RESPID(id, NULL); |
441 | |
|
442 | 0 | if (idlen <= 0 |
443 | | /* Sub-packet for an individual id */ |
444 | 0 | || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) |
445 | 0 | || i2d_OCSP_RESPID(id, &idbytes) != idlen) { |
446 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
447 | 0 | return EXT_RETURN_FAIL; |
448 | 0 | } |
449 | 0 | } |
450 | 0 | if (!WPACKET_close(pkt) |
451 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
452 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
453 | 0 | return EXT_RETURN_FAIL; |
454 | 0 | } |
455 | 0 | if (s->ext.ocsp.exts) { |
456 | 0 | unsigned char *extbytes; |
457 | 0 | int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL); |
458 | |
|
459 | 0 | if (extlen < 0) { |
460 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
461 | 0 | return EXT_RETURN_FAIL; |
462 | 0 | } |
463 | 0 | if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes) |
464 | 0 | || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes) |
465 | 0 | != extlen) { |
466 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
467 | 0 | return EXT_RETURN_FAIL; |
468 | 0 | } |
469 | 0 | } |
470 | 0 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
471 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
472 | 0 | return EXT_RETURN_FAIL; |
473 | 0 | } |
474 | | |
475 | 0 | return EXT_RETURN_SENT; |
476 | 0 | } |
477 | | #endif |
478 | | |
479 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
480 | | EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt, |
481 | | unsigned int context, |
482 | | X509 *x, size_t chainidx) |
483 | 119k | { |
484 | 119k | if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL |
485 | 0 | || !SSL_IS_FIRST_HANDSHAKE(s)) |
486 | 119k | return EXT_RETURN_NOT_SENT; |
487 | | |
488 | | /* |
489 | | * The client advertises an empty extension to indicate its support |
490 | | * for Next Protocol Negotiation |
491 | | */ |
492 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
493 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
494 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
495 | 0 | return EXT_RETURN_FAIL; |
496 | 0 | } |
497 | | |
498 | 0 | return EXT_RETURN_SENT; |
499 | 0 | } |
500 | | #endif |
501 | | |
502 | | EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt, |
503 | | unsigned int context, |
504 | | X509 *x, size_t chainidx) |
505 | 119k | { |
506 | 119k | s->s3.alpn_sent = 0; |
507 | | |
508 | 119k | if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s)) |
509 | 68.3k | return EXT_RETURN_NOT_SENT; |
510 | | |
511 | 51.4k | if (!WPACKET_put_bytes_u16(pkt, |
512 | 51.4k | TLSEXT_TYPE_application_layer_protocol_negotiation) |
513 | | /* Sub-packet ALPN extension */ |
514 | 51.4k | || !WPACKET_start_sub_packet_u16(pkt) |
515 | 51.4k | || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len) |
516 | 51.4k | || !WPACKET_close(pkt)) { |
517 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
518 | 0 | return EXT_RETURN_FAIL; |
519 | 0 | } |
520 | 51.4k | s->s3.alpn_sent = 1; |
521 | | |
522 | 51.4k | return EXT_RETURN_SENT; |
523 | 51.4k | } |
524 | | |
525 | | #ifndef OPENSSL_NO_SRTP |
526 | | EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt, |
527 | | unsigned int context, X509 *x, |
528 | | size_t chainidx) |
529 | 119k | { |
530 | 119k | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
531 | 119k | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl); |
532 | 119k | int i, end; |
533 | | |
534 | 119k | if (clnt == NULL) |
535 | 119k | return EXT_RETURN_NOT_SENT; |
536 | | |
537 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
538 | | /* Sub-packet for SRTP extension */ |
539 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
540 | | /* Sub-packet for the protection profile list */ |
541 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
542 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
543 | 0 | return EXT_RETURN_FAIL; |
544 | 0 | } |
545 | | |
546 | 0 | end = sk_SRTP_PROTECTION_PROFILE_num(clnt); |
547 | 0 | for (i = 0; i < end; i++) { |
548 | 0 | const SRTP_PROTECTION_PROFILE *prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); |
549 | |
|
550 | 0 | if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { |
551 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
552 | 0 | return EXT_RETURN_FAIL; |
553 | 0 | } |
554 | 0 | } |
555 | 0 | if (!WPACKET_close(pkt) |
556 | | /* Add an empty use_mki value */ |
557 | 0 | || !WPACKET_put_bytes_u8(pkt, 0) |
558 | 0 | || !WPACKET_close(pkt)) { |
559 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
560 | 0 | return EXT_RETURN_FAIL; |
561 | 0 | } |
562 | | |
563 | 0 | return EXT_RETURN_SENT; |
564 | 0 | } |
565 | | #endif |
566 | | |
567 | | EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt, |
568 | | unsigned int context, |
569 | | X509 *x, size_t chainidx) |
570 | 119k | { |
571 | 119k | if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) |
572 | 0 | return EXT_RETURN_NOT_SENT; |
573 | | |
574 | 119k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) |
575 | 119k | || !WPACKET_put_bytes_u16(pkt, 0)) { |
576 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
577 | 0 | return EXT_RETURN_FAIL; |
578 | 0 | } |
579 | | |
580 | 119k | return EXT_RETURN_SENT; |
581 | 119k | } |
582 | | |
583 | | #ifndef OPENSSL_NO_CT |
584 | | EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt, |
585 | | unsigned int context, |
586 | | X509 *x, size_t chainidx) |
587 | 119k | { |
588 | 119k | if (s->ct_validation_callback == NULL) |
589 | 119k | return EXT_RETURN_NOT_SENT; |
590 | | |
591 | | /* Not defined for client Certificates */ |
592 | 0 | if (x != NULL) |
593 | 0 | return EXT_RETURN_NOT_SENT; |
594 | | |
595 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp) |
596 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
597 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
598 | 0 | return EXT_RETURN_FAIL; |
599 | 0 | } |
600 | | |
601 | 0 | return EXT_RETURN_SENT; |
602 | 0 | } |
603 | | #endif |
604 | | |
605 | | EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt, |
606 | | unsigned int context, |
607 | | X509 *x, size_t chainidx) |
608 | 119k | { |
609 | 119k | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
610 | 0 | return EXT_RETURN_NOT_SENT; |
611 | | |
612 | 119k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
613 | 119k | || !WPACKET_put_bytes_u16(pkt, 0)) { |
614 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
615 | 0 | return EXT_RETURN_FAIL; |
616 | 0 | } |
617 | | |
618 | 119k | return EXT_RETURN_SENT; |
619 | 119k | } |
620 | | |
621 | | EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt, |
622 | | unsigned int context, X509 *x, |
623 | | size_t chainidx) |
624 | 92.3k | { |
625 | 92.3k | int currv, min_version, max_version, reason; |
626 | | |
627 | 92.3k | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); |
628 | 92.3k | if (reason != 0) { |
629 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); |
630 | 0 | return EXT_RETURN_FAIL; |
631 | 0 | } |
632 | | |
633 | | /* |
634 | | * Don't include this if we can't negotiate TLSv1.3. We can do a straight |
635 | | * comparison here because we will never be called in DTLS. |
636 | | */ |
637 | 92.3k | if (max_version < TLS1_3_VERSION) |
638 | 1.28k | return EXT_RETURN_NOT_SENT; |
639 | | |
640 | 91.0k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) |
641 | 91.0k | || !WPACKET_start_sub_packet_u16(pkt) |
642 | 91.0k | || !WPACKET_start_sub_packet_u8(pkt)) { |
643 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
644 | 0 | return EXT_RETURN_FAIL; |
645 | 0 | } |
646 | | |
647 | 300k | for (currv = max_version; currv >= min_version; currv--) { |
648 | 209k | if (!WPACKET_put_bytes_u16(pkt, currv)) { |
649 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
650 | 0 | return EXT_RETURN_FAIL; |
651 | 0 | } |
652 | 209k | } |
653 | 91.0k | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
654 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
655 | 0 | return EXT_RETURN_FAIL; |
656 | 0 | } |
657 | | |
658 | 91.0k | return EXT_RETURN_SENT; |
659 | 91.0k | } |
660 | | |
661 | | /* |
662 | | * Construct a psk_kex_modes extension. |
663 | | */ |
664 | | EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt, |
665 | | unsigned int context, X509 *x, |
666 | | size_t chainidx) |
667 | 91.0k | { |
668 | 91.0k | #ifndef OPENSSL_NO_TLS1_3 |
669 | 91.0k | int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX; |
670 | | |
671 | 91.0k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes) |
672 | 91.0k | || !WPACKET_start_sub_packet_u16(pkt) |
673 | 91.0k | || !WPACKET_start_sub_packet_u8(pkt) |
674 | 91.0k | || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE) |
675 | 91.0k | || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE)) |
676 | 91.0k | || !WPACKET_close(pkt) |
677 | 91.0k | || !WPACKET_close(pkt)) { |
678 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
679 | 0 | return EXT_RETURN_FAIL; |
680 | 0 | } |
681 | | |
682 | 91.0k | s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE; |
683 | 91.0k | if (nodhe) |
684 | 0 | s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE; |
685 | 91.0k | #endif |
686 | | |
687 | 91.0k | return EXT_RETURN_SENT; |
688 | 91.0k | } |
689 | | |
690 | | #ifndef OPENSSL_NO_TLS1_3 |
691 | | static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int group_id, size_t loop_num) |
692 | 99.5k | { |
693 | 99.5k | unsigned char *encoded_pubkey = NULL; |
694 | 99.5k | EVP_PKEY *key_share_key = NULL; |
695 | 99.5k | size_t encodedlen; |
696 | | |
697 | 99.5k | if (loop_num < s->s3.tmp.num_ks_pkey) { |
698 | 50 | if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING) |
699 | 50 | || !ossl_assert(s->s3.tmp.ks_pkey[loop_num] != NULL)) { |
700 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
701 | 0 | return 0; |
702 | 0 | } |
703 | | /* |
704 | | * Could happen if we got an HRR that wasn't requesting a new key_share |
705 | | */ |
706 | 50 | key_share_key = s->s3.tmp.ks_pkey[loop_num]; |
707 | 99.4k | } else { |
708 | 99.4k | key_share_key = ssl_generate_pkey_group(s, group_id); |
709 | 99.4k | if (key_share_key == NULL) { |
710 | | /* SSLfatal() already called */ |
711 | 0 | return 0; |
712 | 0 | } |
713 | 99.4k | } |
714 | | |
715 | | /* Encode the public key. */ |
716 | 99.5k | encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key, |
717 | 99.5k | &encoded_pubkey); |
718 | 99.5k | if (encodedlen == 0) { |
719 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
720 | 0 | goto err; |
721 | 0 | } |
722 | | |
723 | | /* Create KeyShareEntry */ |
724 | 99.5k | if (!WPACKET_put_bytes_u16(pkt, group_id) |
725 | 99.5k | || !WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encodedlen)) { |
726 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
727 | 0 | goto err; |
728 | 0 | } |
729 | | |
730 | | /* For backward compatibility, we use the first valid group to add a key share */ |
731 | 99.5k | if (loop_num == 0) { |
732 | 49.8k | s->s3.tmp.pkey = key_share_key; |
733 | 49.8k | s->s3.group_id = group_id; |
734 | 49.8k | } |
735 | | /* We ensure in t1_lib.c that the loop number does not exceed OPENSSL_CLIENT_MAX_KEY_SHARES */ |
736 | 99.5k | s->s3.tmp.ks_pkey[loop_num] = key_share_key; |
737 | 99.5k | s->s3.tmp.ks_group_id[loop_num] = group_id; |
738 | 99.5k | if (loop_num >= s->s3.tmp.num_ks_pkey) |
739 | 99.4k | s->s3.tmp.num_ks_pkey++; |
740 | | |
741 | 99.5k | OPENSSL_free(encoded_pubkey); |
742 | | |
743 | 99.5k | return 1; |
744 | 0 | err: |
745 | 0 | if (key_share_key != s->s3.tmp.ks_pkey[loop_num]) |
746 | 0 | EVP_PKEY_free(key_share_key); |
747 | 0 | OPENSSL_free(encoded_pubkey); |
748 | 0 | return 0; |
749 | 99.5k | } |
750 | | #endif |
751 | | |
752 | | EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt, |
753 | | unsigned int context, X509 *x, |
754 | | size_t chainidx) |
755 | 49.8k | { |
756 | 49.8k | #ifndef OPENSSL_NO_TLS1_3 |
757 | 49.8k | size_t i, num_groups = 0; |
758 | 49.8k | const uint16_t *pgroups = NULL; |
759 | 49.8k | uint16_t group_id = 0; |
760 | 49.8k | int add_only_one = 0; |
761 | 49.8k | size_t valid_keyshare = 0; |
762 | | |
763 | | /* key_share extension */ |
764 | 49.8k | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) |
765 | | /* Extension data sub-packet */ |
766 | 49.8k | || !WPACKET_start_sub_packet_u16(pkt) |
767 | | /* KeyShare list sub-packet */ |
768 | 49.8k | || !WPACKET_start_sub_packet_u16(pkt)) { |
769 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
770 | 0 | return EXT_RETURN_FAIL; |
771 | 0 | } |
772 | | |
773 | 49.8k | tls1_get_requested_keyshare_groups(s, &pgroups, &num_groups); |
774 | 49.8k | if (num_groups == 1 && pgroups[0] == 0) { /* Indication that no * prefix was used */ |
775 | 0 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
776 | 0 | add_only_one = 1; |
777 | 0 | } |
778 | | |
779 | | /* If neither the default nor the keyshares have any entry --> fatal */ |
780 | 49.8k | if (num_groups == 0) { |
781 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
782 | 0 | return EXT_RETURN_FAIL; |
783 | 0 | } |
784 | | |
785 | | /* Add key shares */ |
786 | | |
787 | 49.8k | if (s->s3.group_id != 0 && s->s3.tmp.pkey == NULL) { |
788 | | /* new, single key share */ |
789 | 203 | group_id = s->s3.group_id; |
790 | 203 | s->s3.tmp.num_ks_pkey = 0; |
791 | 203 | if (!add_key_share(s, pkt, group_id, 0)) { |
792 | | /* SSLfatal() already called */ |
793 | 0 | return EXT_RETURN_FAIL; |
794 | 0 | } |
795 | 203 | valid_keyshare++; |
796 | 49.6k | } else { |
797 | 49.6k | if (s->ext.supportedgroups == NULL) /* use default */ |
798 | 0 | add_only_one = 1; |
799 | | |
800 | 148k | for (i = 0; i < num_groups; i++) { |
801 | 99.3k | if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED)) |
802 | 0 | continue; |
803 | 99.3k | if (!tls_valid_group(s, pgroups[i], TLS1_3_VERSION, TLS1_3_VERSION, |
804 | 99.3k | NULL, NULL)) |
805 | 0 | continue; |
806 | | |
807 | 99.3k | group_id = pgroups[i]; |
808 | | |
809 | 99.3k | if (group_id == 0) { |
810 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
811 | 0 | return EXT_RETURN_FAIL; |
812 | 0 | } |
813 | 99.3k | if (!add_key_share(s, pkt, group_id, valid_keyshare)) { |
814 | | /* SSLfatal() already called */ |
815 | 0 | return EXT_RETURN_FAIL; |
816 | 0 | } |
817 | 99.3k | valid_keyshare++; |
818 | 99.3k | if (add_only_one) |
819 | 0 | break; |
820 | 99.3k | } |
821 | 49.6k | } |
822 | | |
823 | 49.8k | if (valid_keyshare == 0) { |
824 | | /* No key shares were allowed */ |
825 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); |
826 | 0 | return EXT_RETURN_FAIL; |
827 | 0 | } |
828 | | |
829 | 49.8k | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
830 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
831 | 0 | return EXT_RETURN_FAIL; |
832 | 0 | } |
833 | 49.8k | return EXT_RETURN_SENT; |
834 | | #else |
835 | | return EXT_RETURN_NOT_SENT; |
836 | | #endif |
837 | 49.8k | } |
838 | | |
839 | | EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt, |
840 | | unsigned int context, |
841 | | X509 *x, size_t chainidx) |
842 | 91.0k | { |
843 | 91.0k | EXT_RETURN ret = EXT_RETURN_FAIL; |
844 | | |
845 | | /* Should only be set if we've had an HRR */ |
846 | 91.0k | if (s->ext.tls13_cookie_len == 0) |
847 | 90.9k | return EXT_RETURN_NOT_SENT; |
848 | | |
849 | 35 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie) |
850 | | /* Extension data sub-packet */ |
851 | 35 | || !WPACKET_start_sub_packet_u16(pkt) |
852 | 35 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie, |
853 | 35 | s->ext.tls13_cookie_len) |
854 | 35 | || !WPACKET_close(pkt)) { |
855 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
856 | 0 | goto end; |
857 | 0 | } |
858 | | |
859 | 35 | ret = EXT_RETURN_SENT; |
860 | 35 | end: |
861 | 35 | OPENSSL_free(s->ext.tls13_cookie); |
862 | 35 | s->ext.tls13_cookie = NULL; |
863 | 35 | s->ext.tls13_cookie_len = 0; |
864 | | |
865 | 35 | return ret; |
866 | 35 | } |
867 | | |
868 | | EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt, |
869 | | unsigned int context, X509 *x, |
870 | | size_t chainidx) |
871 | 91.0k | { |
872 | 91.0k | #ifndef OPENSSL_NO_PSK |
873 | 91.0k | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
874 | 91.0k | #endif /* OPENSSL_NO_PSK */ |
875 | 91.0k | const unsigned char *id = NULL; |
876 | 91.0k | size_t idlen = 0; |
877 | 91.0k | SSL_SESSION *psksess = NULL; |
878 | 91.0k | SSL_SESSION *edsess = NULL; |
879 | 91.0k | const EVP_MD *handmd = NULL; |
880 | 91.0k | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
881 | | |
882 | 91.0k | if (s->hello_retry_request == SSL_HRR_PENDING) |
883 | 492 | handmd = ssl_handshake_md(s); |
884 | | |
885 | 91.0k | if (s->psk_use_session_cb != NULL |
886 | 0 | && (!s->psk_use_session_cb(ussl, handmd, &id, &idlen, &psksess) |
887 | 0 | || (psksess != NULL |
888 | 0 | && psksess->ssl_version != TLS1_3_VERSION))) { |
889 | 0 | SSL_SESSION_free(psksess); |
890 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
891 | 0 | return EXT_RETURN_FAIL; |
892 | 0 | } |
893 | | |
894 | 91.0k | #ifndef OPENSSL_NO_PSK |
895 | 91.0k | if (psksess == NULL && s->psk_client_callback != NULL) { |
896 | 0 | unsigned char psk[PSK_MAX_PSK_LEN]; |
897 | 0 | size_t psklen = 0; |
898 | |
|
899 | 0 | memset(identity, 0, sizeof(identity)); |
900 | 0 | psklen = s->psk_client_callback(ussl, NULL, |
901 | 0 | identity, sizeof(identity) - 1, |
902 | 0 | psk, sizeof(psk)); |
903 | |
|
904 | 0 | if (psklen > PSK_MAX_PSK_LEN) { |
905 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR); |
906 | 0 | return EXT_RETURN_FAIL; |
907 | 0 | } else if (psklen > 0) { |
908 | 0 | const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; |
909 | 0 | const SSL_CIPHER *cipher; |
910 | |
|
911 | 0 | idlen = strlen(identity); |
912 | 0 | if (idlen > PSK_MAX_IDENTITY_LEN) { |
913 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
914 | 0 | return EXT_RETURN_FAIL; |
915 | 0 | } |
916 | 0 | id = (unsigned char *)identity; |
917 | | |
918 | | /* |
919 | | * We found a PSK using an old style callback. We don't know |
920 | | * the digest so we default to SHA256 as per the TLSv1.3 spec |
921 | | */ |
922 | 0 | cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s), |
923 | 0 | tls13_aes128gcmsha256_id); |
924 | 0 | if (cipher == NULL) { |
925 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
926 | 0 | return EXT_RETURN_FAIL; |
927 | 0 | } |
928 | | |
929 | 0 | psksess = SSL_SESSION_new(); |
930 | 0 | if (psksess == NULL |
931 | 0 | || !SSL_SESSION_set1_master_key(psksess, psk, psklen) |
932 | 0 | || !SSL_SESSION_set_cipher(psksess, cipher) |
933 | 0 | || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) { |
934 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
935 | 0 | OPENSSL_cleanse(psk, psklen); |
936 | 0 | return EXT_RETURN_FAIL; |
937 | 0 | } |
938 | 0 | OPENSSL_cleanse(psk, psklen); |
939 | 0 | } |
940 | 0 | } |
941 | 91.0k | #endif /* OPENSSL_NO_PSK */ |
942 | | |
943 | 91.0k | SSL_SESSION_free(s->psksession); |
944 | 91.0k | s->psksession = psksess; |
945 | 91.0k | if (psksess != NULL) { |
946 | 0 | OPENSSL_free(s->psksession_id); |
947 | 0 | s->psksession_id = OPENSSL_memdup(id, idlen); |
948 | 0 | if (s->psksession_id == NULL) { |
949 | 0 | s->psksession_id_len = 0; |
950 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
951 | 0 | return EXT_RETURN_FAIL; |
952 | 0 | } |
953 | 0 | s->psksession_id_len = idlen; |
954 | 0 | } |
955 | | |
956 | 91.0k | if (s->early_data_state != SSL_EARLY_DATA_CONNECTING |
957 | 0 | || (s->session->ext.max_early_data == 0 |
958 | 91.0k | && (psksess == NULL || psksess->ext.max_early_data == 0))) { |
959 | 91.0k | s->max_early_data = 0; |
960 | 91.0k | return EXT_RETURN_NOT_SENT; |
961 | 91.0k | } |
962 | 0 | edsess = s->session->ext.max_early_data != 0 ? s->session : psksess; |
963 | 0 | s->max_early_data = edsess->ext.max_early_data; |
964 | |
|
965 | 0 | if (edsess->ext.hostname != NULL) { |
966 | 0 | if (s->ext.hostname == NULL |
967 | 0 | || (s->ext.hostname != NULL |
968 | 0 | && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) { |
969 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
970 | 0 | SSL_R_INCONSISTENT_EARLY_DATA_SNI); |
971 | 0 | return EXT_RETURN_FAIL; |
972 | 0 | } |
973 | 0 | } |
974 | | |
975 | 0 | if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) { |
976 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN); |
977 | 0 | return EXT_RETURN_FAIL; |
978 | 0 | } |
979 | | |
980 | | /* |
981 | | * Verify that we are offering an ALPN protocol consistent with the early |
982 | | * data. |
983 | | */ |
984 | 0 | if (edsess->ext.alpn_selected != NULL) { |
985 | 0 | PACKET prots, alpnpkt; |
986 | 0 | int found = 0; |
987 | |
|
988 | 0 | if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) { |
989 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
990 | 0 | return EXT_RETURN_FAIL; |
991 | 0 | } |
992 | 0 | while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) { |
993 | 0 | if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected, |
994 | 0 | edsess->ext.alpn_selected_len)) { |
995 | 0 | found = 1; |
996 | 0 | break; |
997 | 0 | } |
998 | 0 | } |
999 | 0 | if (!found) { |
1000 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
1001 | 0 | SSL_R_INCONSISTENT_EARLY_DATA_ALPN); |
1002 | 0 | return EXT_RETURN_FAIL; |
1003 | 0 | } |
1004 | 0 | } |
1005 | | |
1006 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) |
1007 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1008 | 0 | || !WPACKET_close(pkt)) { |
1009 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1010 | 0 | return EXT_RETURN_FAIL; |
1011 | 0 | } |
1012 | | |
1013 | | /* |
1014 | | * We set this to rejected here. Later, if the server acknowledges the |
1015 | | * extension, we set it to accepted. |
1016 | | */ |
1017 | 0 | s->ext.early_data = SSL_EARLY_DATA_REJECTED; |
1018 | 0 | s->ext.early_data_ok = 1; |
1019 | |
|
1020 | 0 | return EXT_RETURN_SENT; |
1021 | 0 | } |
1022 | | |
1023 | 0 | #define F5_WORKAROUND_MIN_MSG_LEN 0xff |
1024 | 0 | #define F5_WORKAROUND_MAX_MSG_LEN 0x200 |
1025 | | |
1026 | | /* |
1027 | | * PSK pre binder overhead = |
1028 | | * 2 bytes for TLSEXT_TYPE_psk |
1029 | | * 2 bytes for extension length |
1030 | | * 2 bytes for identities list length |
1031 | | * 2 bytes for identity length |
1032 | | * 4 bytes for obfuscated_ticket_age |
1033 | | * 2 bytes for binder list length |
1034 | | * 1 byte for binder length |
1035 | | * The above excludes the number of bytes for the identity itself and the |
1036 | | * subsequent binder bytes |
1037 | | */ |
1038 | 0 | #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1) |
1039 | | |
1040 | | EXT_RETURN tls_construct_ctos_padding(SSL_CONNECTION *s, WPACKET *pkt, |
1041 | | unsigned int context, X509 *x, |
1042 | | size_t chainidx) |
1043 | 89.5k | { |
1044 | 89.5k | unsigned char *padbytes; |
1045 | 89.5k | size_t hlen; |
1046 | | |
1047 | 89.5k | if ((s->options & SSL_OP_TLSEXT_PADDING) == 0) |
1048 | 89.5k | return EXT_RETURN_NOT_SENT; |
1049 | | |
1050 | | /* |
1051 | | * Add padding to workaround bugs in F5 terminators. See RFC7685. |
1052 | | * This code calculates the length of all extensions added so far but |
1053 | | * excludes the PSK extension (because that MUST be written last). Therefore |
1054 | | * this extension MUST always appear second to last. |
1055 | | */ |
1056 | 0 | if (!WPACKET_get_total_written(pkt, &hlen)) { |
1057 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1058 | 0 | return EXT_RETURN_FAIL; |
1059 | 0 | } |
1060 | | |
1061 | | /* |
1062 | | * If we're going to send a PSK then that will be written out after this |
1063 | | * extension, so we need to calculate how long it is going to be. |
1064 | | */ |
1065 | 0 | if (s->session->ssl_version == TLS1_3_VERSION |
1066 | 0 | && s->session->ext.ticklen != 0 |
1067 | 0 | && s->session->cipher != NULL) { |
1068 | 0 | const EVP_MD *md = ssl_md(SSL_CONNECTION_GET_CTX(s), |
1069 | 0 | s->session->cipher->algorithm2); |
1070 | |
|
1071 | 0 | if (md != NULL) { |
1072 | | /* |
1073 | | * Add the fixed PSK overhead, the identity length and the binder |
1074 | | * length. |
1075 | | */ |
1076 | 0 | int md_size = EVP_MD_get_size(md); |
1077 | |
|
1078 | 0 | if (md_size <= 0) |
1079 | 0 | return EXT_RETURN_FAIL; |
1080 | 0 | hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen |
1081 | 0 | + md_size; |
1082 | 0 | } |
1083 | 0 | } |
1084 | | |
1085 | 0 | if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) { |
1086 | | /* Calculate the amount of padding we need to add */ |
1087 | 0 | hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen; |
1088 | | |
1089 | | /* |
1090 | | * Take off the size of extension header itself (2 bytes for type and |
1091 | | * 2 bytes for length bytes), but ensure that the extension is at least |
1092 | | * 1 byte long so as not to have an empty extension last (WebSphere 7.x, |
1093 | | * 8.x are intolerant of that condition) |
1094 | | */ |
1095 | 0 | if (hlen > 4) |
1096 | 0 | hlen -= 4; |
1097 | 0 | else |
1098 | 0 | hlen = 1; |
1099 | |
|
1100 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding) |
1101 | 0 | || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) { |
1102 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1103 | 0 | return EXT_RETURN_FAIL; |
1104 | 0 | } |
1105 | 0 | memset(padbytes, 0, hlen); |
1106 | 0 | } |
1107 | | |
1108 | 0 | return EXT_RETURN_SENT; |
1109 | 0 | } |
1110 | | |
1111 | | /* |
1112 | | * Construct the pre_shared_key extension |
1113 | | */ |
1114 | | EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt, |
1115 | | unsigned int context, |
1116 | | X509 *x, size_t chainidx) |
1117 | 67.1k | { |
1118 | 67.1k | #ifndef OPENSSL_NO_TLS1_3 |
1119 | 67.1k | uint32_t agesec, agems = 0; |
1120 | 67.1k | size_t binderoffset, msglen; |
1121 | 67.1k | int reshashsize = 0, pskhashsize = 0; |
1122 | 67.1k | unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL; |
1123 | 67.1k | const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL; |
1124 | 67.1k | int dores = 0; |
1125 | 67.1k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1126 | 67.1k | OSSL_TIME t; |
1127 | | |
1128 | 67.1k | s->ext.tick_identity = 0; |
1129 | | |
1130 | | /* |
1131 | | * Note: At this stage of the code we only support adding a single |
1132 | | * resumption PSK. If we add support for multiple PSKs then the length |
1133 | | * calculations in the padding extension will need to be adjusted. |
1134 | | */ |
1135 | | |
1136 | | /* |
1137 | | * If this is an incompatible or new session then we have nothing to resume |
1138 | | * so don't add this extension. |
1139 | | */ |
1140 | 67.1k | if (s->session->ssl_version != TLS1_3_VERSION |
1141 | 67.1k | || (s->session->ext.ticklen == 0 && s->psksession == NULL)) |
1142 | 67.1k | return EXT_RETURN_NOT_SENT; |
1143 | | |
1144 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) |
1145 | 0 | handmd = ssl_handshake_md(s); |
1146 | |
|
1147 | 0 | if (s->session->ext.ticklen != 0) { |
1148 | | /* Get the digest associated with the ciphersuite in the session */ |
1149 | 0 | if (s->session->cipher == NULL) { |
1150 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1151 | 0 | return EXT_RETURN_FAIL; |
1152 | 0 | } |
1153 | 0 | mdres = ssl_md(sctx, s->session->cipher->algorithm2); |
1154 | 0 | if (mdres == NULL) { |
1155 | | /* |
1156 | | * Don't recognize this cipher so we can't use the session. |
1157 | | * Ignore it |
1158 | | */ |
1159 | 0 | goto dopsksess; |
1160 | 0 | } |
1161 | | |
1162 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) { |
1163 | | /* |
1164 | | * Selected ciphersuite hash does not match the hash for the session |
1165 | | * so we can't use it. |
1166 | | */ |
1167 | 0 | goto dopsksess; |
1168 | 0 | } |
1169 | | |
1170 | | /* |
1171 | | * Technically the C standard just says time() returns a time_t and says |
1172 | | * nothing about the encoding of that type. In practice most |
1173 | | * implementations follow POSIX which holds it as an integral type in |
1174 | | * seconds since epoch. We've already made the assumption that we can do |
1175 | | * this in multiple places in the code, so portability shouldn't be an |
1176 | | * issue. |
1177 | | */ |
1178 | 0 | t = ossl_time_subtract(ossl_time_now(), s->session->time); |
1179 | 0 | agesec = (uint32_t)ossl_time2seconds(t); |
1180 | | /* |
1181 | | * We calculate the age in seconds but the server may work in ms. Due to |
1182 | | * rounding errors we could overestimate the age by up to 1s. It is |
1183 | | * better to underestimate it. Otherwise, if the RTT is very short, when |
1184 | | * the server calculates the age reported by the client it could be |
1185 | | * bigger than the age calculated on the server - which should never |
1186 | | * happen. |
1187 | | */ |
1188 | 0 | if (agesec > 0) |
1189 | 0 | agesec--; |
1190 | |
|
1191 | 0 | if (s->session->ext.tick_lifetime_hint < agesec) { |
1192 | | /* Ticket is too old. Ignore it. */ |
1193 | 0 | goto dopsksess; |
1194 | 0 | } |
1195 | | |
1196 | | /* |
1197 | | * Calculate age in ms. We're just doing it to nearest second. Should be |
1198 | | * good enough. |
1199 | | */ |
1200 | 0 | agems = agesec * (uint32_t)1000; |
1201 | |
|
1202 | 0 | if (agesec != 0 && agems / (uint32_t)1000 != agesec) { |
1203 | | /* |
1204 | | * Overflow. Shouldn't happen unless this is a *really* old session. |
1205 | | * If so we just ignore it. |
1206 | | */ |
1207 | 0 | goto dopsksess; |
1208 | 0 | } |
1209 | | |
1210 | | /* |
1211 | | * Obfuscate the age. Overflow here is fine, this addition is supposed |
1212 | | * to be mod 2^32. |
1213 | | */ |
1214 | 0 | agems += s->session->ext.tick_age_add; |
1215 | |
|
1216 | 0 | reshashsize = EVP_MD_get_size(mdres); |
1217 | 0 | if (reshashsize <= 0) |
1218 | 0 | goto dopsksess; |
1219 | 0 | s->ext.tick_identity++; |
1220 | 0 | dores = 1; |
1221 | 0 | } |
1222 | | |
1223 | 0 | dopsksess: |
1224 | 0 | if (!dores && s->psksession == NULL) |
1225 | 0 | return EXT_RETURN_NOT_SENT; |
1226 | | |
1227 | 0 | if (s->psksession != NULL) { |
1228 | 0 | mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2); |
1229 | 0 | if (mdpsk == NULL) { |
1230 | | /* |
1231 | | * Don't recognize this cipher so we can't use the session. |
1232 | | * If this happens it's an application bug. |
1233 | | */ |
1234 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1235 | 0 | return EXT_RETURN_FAIL; |
1236 | 0 | } |
1237 | | |
1238 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) { |
1239 | | /* |
1240 | | * Selected ciphersuite hash does not match the hash for the PSK |
1241 | | * session. This is an application bug. |
1242 | | */ |
1243 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1244 | 0 | return EXT_RETURN_FAIL; |
1245 | 0 | } |
1246 | | |
1247 | 0 | pskhashsize = EVP_MD_get_size(mdpsk); |
1248 | 0 | if (pskhashsize <= 0) { |
1249 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); |
1250 | 0 | return EXT_RETURN_FAIL; |
1251 | 0 | } |
1252 | 0 | } |
1253 | | |
1254 | | /* Create the extension, but skip over the binder for now */ |
1255 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk) |
1256 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1257 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
1258 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1259 | 0 | return EXT_RETURN_FAIL; |
1260 | 0 | } |
1261 | | |
1262 | 0 | if (dores) { |
1263 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, |
1264 | 0 | s->session->ext.ticklen) |
1265 | 0 | || !WPACKET_put_bytes_u32(pkt, agems)) { |
1266 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1267 | 0 | return EXT_RETURN_FAIL; |
1268 | 0 | } |
1269 | 0 | } |
1270 | | |
1271 | 0 | if (s->psksession != NULL) { |
1272 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id, |
1273 | 0 | s->psksession_id_len) |
1274 | 0 | || !WPACKET_put_bytes_u32(pkt, 0)) { |
1275 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1276 | 0 | return EXT_RETURN_FAIL; |
1277 | 0 | } |
1278 | 0 | s->ext.tick_identity++; |
1279 | 0 | } |
1280 | | |
1281 | 0 | if (!WPACKET_close(pkt) |
1282 | 0 | || !WPACKET_get_total_written(pkt, &binderoffset) |
1283 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1284 | 0 | || (dores |
1285 | 0 | && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder)) |
1286 | 0 | || (s->psksession != NULL |
1287 | 0 | && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder)) |
1288 | 0 | || !WPACKET_close(pkt) |
1289 | 0 | || !WPACKET_close(pkt) |
1290 | 0 | || !WPACKET_get_total_written(pkt, &msglen) |
1291 | | /* |
1292 | | * We need to fill in all the sub-packet lengths now so we can |
1293 | | * calculate the HMAC of the message up to the binders |
1294 | | */ |
1295 | 0 | || !WPACKET_fill_lengths(pkt)) { |
1296 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1297 | 0 | return EXT_RETURN_FAIL; |
1298 | 0 | } |
1299 | | |
1300 | 0 | msgstart = WPACKET_get_curr(pkt) - msglen; |
1301 | |
|
1302 | 0 | if (dores |
1303 | 0 | && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL, |
1304 | 0 | resbinder, s->session, 1, 0) |
1305 | 0 | != 1) { |
1306 | | /* SSLfatal() already called */ |
1307 | 0 | return EXT_RETURN_FAIL; |
1308 | 0 | } |
1309 | | |
1310 | 0 | if (s->psksession != NULL |
1311 | 0 | && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL, |
1312 | 0 | pskbinder, s->psksession, 1, 1) |
1313 | 0 | != 1) { |
1314 | | /* SSLfatal() already called */ |
1315 | 0 | return EXT_RETURN_FAIL; |
1316 | 0 | } |
1317 | | |
1318 | 0 | return EXT_RETURN_SENT; |
1319 | | #else |
1320 | | return EXT_RETURN_NOT_SENT; |
1321 | | #endif |
1322 | 0 | } |
1323 | | |
1324 | | EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt, |
1325 | | ossl_unused unsigned int context, |
1326 | | ossl_unused X509 *x, |
1327 | | ossl_unused size_t chainidx) |
1328 | 91.0k | { |
1329 | 91.0k | #ifndef OPENSSL_NO_TLS1_3 |
1330 | 91.0k | if (!s->pha_enabled) |
1331 | 91.0k | return EXT_RETURN_NOT_SENT; |
1332 | | |
1333 | | /* construct extension - 0 length, no contents */ |
1334 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth) |
1335 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1336 | 0 | || !WPACKET_close(pkt)) { |
1337 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1338 | 0 | return EXT_RETURN_FAIL; |
1339 | 0 | } |
1340 | | |
1341 | 0 | s->post_handshake_auth = SSL_PHA_EXT_SENT; |
1342 | |
|
1343 | 0 | return EXT_RETURN_SENT; |
1344 | | #else |
1345 | | return EXT_RETURN_NOT_SENT; |
1346 | | #endif |
1347 | 0 | } |
1348 | | |
1349 | | /* |
1350 | | * Parse the server's renegotiation binding and abort if it's not right |
1351 | | */ |
1352 | | int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt, |
1353 | | unsigned int context, |
1354 | | X509 *x, size_t chainidx) |
1355 | 47.9k | { |
1356 | 47.9k | size_t expected_len = s->s3.previous_client_finished_len |
1357 | 47.9k | + s->s3.previous_server_finished_len; |
1358 | 47.9k | size_t ilen; |
1359 | 47.9k | const unsigned char *data; |
1360 | | |
1361 | | /* Check for logic errors */ |
1362 | 47.9k | if (!ossl_assert(expected_len == 0 |
1363 | 47.9k | || s->s3.previous_client_finished_len != 0) |
1364 | 47.9k | || !ossl_assert(expected_len == 0 |
1365 | 47.9k | || s->s3.previous_server_finished_len != 0)) { |
1366 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1367 | 0 | return 0; |
1368 | 0 | } |
1369 | | |
1370 | | /* Parse the length byte */ |
1371 | 47.9k | if (!PACKET_get_1_len(pkt, &ilen)) { |
1372 | 12 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
1373 | 12 | return 0; |
1374 | 12 | } |
1375 | | |
1376 | | /* Consistency check */ |
1377 | 47.9k | if (PACKET_remaining(pkt) != ilen) { |
1378 | 84 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
1379 | 84 | return 0; |
1380 | 84 | } |
1381 | | |
1382 | | /* Check that the extension matches */ |
1383 | 47.8k | if (ilen != expected_len) { |
1384 | 32 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
1385 | 32 | return 0; |
1386 | 32 | } |
1387 | | |
1388 | 47.8k | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len) |
1389 | 47.8k | || memcmp(data, s->s3.previous_client_finished, |
1390 | 47.8k | s->s3.previous_client_finished_len) |
1391 | 47.8k | != 0) { |
1392 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
1393 | 0 | return 0; |
1394 | 0 | } |
1395 | | |
1396 | 47.8k | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len) |
1397 | 47.8k | || memcmp(data, s->s3.previous_server_finished, |
1398 | 47.8k | s->s3.previous_server_finished_len) |
1399 | 47.8k | != 0) { |
1400 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); |
1401 | 0 | return 0; |
1402 | 0 | } |
1403 | 47.8k | s->s3.send_connection_binding = 1; |
1404 | | |
1405 | 47.8k | return 1; |
1406 | 47.8k | } |
1407 | | |
1408 | | /* Parse the server's max fragment len extension packet */ |
1409 | | int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt, |
1410 | | unsigned int context, |
1411 | | X509 *x, size_t chainidx) |
1412 | 0 | { |
1413 | 0 | unsigned int value; |
1414 | |
|
1415 | 0 | if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { |
1416 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1417 | 0 | return 0; |
1418 | 0 | } |
1419 | | |
1420 | | /* |value| should contains a valid max-fragment-length code. */ |
1421 | 0 | if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { |
1422 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1423 | 0 | SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
1424 | 0 | return 0; |
1425 | 0 | } |
1426 | | |
1427 | | /* Must be the same value as client-configured one who was sent to server */ |
1428 | | /*- |
1429 | | * RFC 6066: if a client receives a maximum fragment length negotiation |
1430 | | * response that differs from the length it requested, ... |
1431 | | * It must abort with SSL_AD_ILLEGAL_PARAMETER alert |
1432 | | */ |
1433 | 0 | if (value != s->ext.max_fragment_len_mode) { |
1434 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1435 | 0 | SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
1436 | 0 | return 0; |
1437 | 0 | } |
1438 | | |
1439 | | /* |
1440 | | * Maximum Fragment Length Negotiation succeeded. |
1441 | | * The negotiated Maximum Fragment Length is binding now. |
1442 | | */ |
1443 | 0 | s->session->ext.max_fragment_len_mode = value; |
1444 | |
|
1445 | 0 | return 1; |
1446 | 0 | } |
1447 | | |
1448 | | int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt, |
1449 | | unsigned int context, |
1450 | | X509 *x, size_t chainidx) |
1451 | 5.87k | { |
1452 | 5.87k | if (s->ext.hostname == NULL) { |
1453 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1454 | 0 | return 0; |
1455 | 0 | } |
1456 | | |
1457 | 5.87k | if (PACKET_remaining(pkt) > 0) { |
1458 | 22 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1459 | 22 | return 0; |
1460 | 22 | } |
1461 | | |
1462 | 5.85k | if (!s->hit) { |
1463 | 5.84k | if (s->session->ext.hostname != NULL) { |
1464 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1465 | 0 | return 0; |
1466 | 0 | } |
1467 | 5.84k | s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname); |
1468 | 5.84k | if (s->session->ext.hostname == NULL) { |
1469 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1470 | 0 | return 0; |
1471 | 0 | } |
1472 | 5.84k | } |
1473 | | |
1474 | 5.85k | return 1; |
1475 | 5.85k | } |
1476 | | |
1477 | | int tls_parse_stoc_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt, |
1478 | | unsigned int context, |
1479 | | X509 *x, size_t chainidx) |
1480 | 3.50k | { |
1481 | 3.50k | size_t ecpointformats_len; |
1482 | 3.50k | PACKET ecptformatlist; |
1483 | | |
1484 | 3.50k | if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) { |
1485 | 105 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1486 | 105 | return 0; |
1487 | 105 | } |
1488 | 3.40k | if (!s->hit) { |
1489 | 3.40k | ecpointformats_len = PACKET_remaining(&ecptformatlist); |
1490 | 3.40k | if (ecpointformats_len == 0) { |
1491 | 13 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
1492 | 13 | return 0; |
1493 | 13 | } |
1494 | | |
1495 | 3.38k | s->ext.peer_ecpointformats_len = 0; |
1496 | 3.38k | OPENSSL_free(s->ext.peer_ecpointformats); |
1497 | 3.38k | s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len); |
1498 | 3.38k | if (s->ext.peer_ecpointformats == NULL) { |
1499 | 0 | s->ext.peer_ecpointformats_len = 0; |
1500 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1501 | 0 | return 0; |
1502 | 0 | } |
1503 | | |
1504 | 3.38k | s->ext.peer_ecpointformats_len = ecpointformats_len; |
1505 | | |
1506 | 3.38k | if (!PACKET_copy_bytes(&ecptformatlist, |
1507 | 3.38k | s->ext.peer_ecpointformats, |
1508 | 3.38k | ecpointformats_len)) { |
1509 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1510 | 0 | return 0; |
1511 | 0 | } |
1512 | 3.38k | } |
1513 | | |
1514 | 3.38k | return 1; |
1515 | 3.40k | } |
1516 | | |
1517 | | int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt, |
1518 | | unsigned int context, |
1519 | | X509 *x, size_t chainidx) |
1520 | 11.3k | { |
1521 | 11.3k | SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); |
1522 | | |
1523 | 11.3k | 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)) { |
1524 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); |
1525 | 0 | return 0; |
1526 | 0 | } |
1527 | | |
1528 | 11.3k | if (!tls_use_ticket(s)) { |
1529 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1530 | 0 | return 0; |
1531 | 0 | } |
1532 | 11.3k | if (PACKET_remaining(pkt) > 0) { |
1533 | 14 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1534 | 14 | return 0; |
1535 | 14 | } |
1536 | | |
1537 | 11.3k | s->ext.ticket_expected = 1; |
1538 | | |
1539 | 11.3k | return 1; |
1540 | 11.3k | } |
1541 | | |
1542 | | #ifndef OPENSSL_NO_OCSP |
1543 | | int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt, |
1544 | | unsigned int context, |
1545 | | X509 *x, size_t chainidx) |
1546 | 2 | { |
1547 | 2 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { |
1548 | | /* We ignore this if the server sends a CertificateRequest */ |
1549 | 2 | return 1; |
1550 | 2 | } |
1551 | | |
1552 | | /* |
1553 | | * MUST only be sent if we've requested a status |
1554 | | * request message. In TLS <= 1.2 it must also be empty. |
1555 | | */ |
1556 | 0 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { |
1557 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1558 | 0 | return 0; |
1559 | 0 | } |
1560 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) && PACKET_remaining(pkt) > 0) { |
1561 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1562 | 0 | return 0; |
1563 | 0 | } |
1564 | | |
1565 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
1566 | | /* SSLfatal() already called */ |
1567 | 0 | return tls_process_cert_status_body(s, chainidx, pkt); |
1568 | 0 | } |
1569 | | |
1570 | | /* Set flag to expect CertificateStatus message */ |
1571 | 0 | s->ext.status_expected = 1; |
1572 | |
|
1573 | 0 | return 1; |
1574 | 0 | } |
1575 | | #endif |
1576 | | |
1577 | | #ifndef OPENSSL_NO_CT |
1578 | | int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1579 | | X509 *x, size_t chainidx) |
1580 | 24 | { |
1581 | 24 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { |
1582 | | /* We ignore this if the server sends it in a CertificateRequest */ |
1583 | 6 | return 1; |
1584 | 6 | } |
1585 | | |
1586 | | /* |
1587 | | * Only take it if we asked for it - i.e if there is no CT validation |
1588 | | * callback set, then a custom extension MAY be processing it, so we |
1589 | | * need to let control continue to flow to that. |
1590 | | */ |
1591 | 18 | if (s->ct_validation_callback != NULL) { |
1592 | 0 | size_t size = PACKET_remaining(pkt); |
1593 | | |
1594 | | /* Simply copy it off for later processing */ |
1595 | 0 | OPENSSL_free(s->ext.scts); |
1596 | 0 | s->ext.scts = NULL; |
1597 | |
|
1598 | 0 | s->ext.scts_len = (uint16_t)size; |
1599 | 0 | if (size > 0) { |
1600 | 0 | s->ext.scts = OPENSSL_malloc(size); |
1601 | 0 | if (s->ext.scts == NULL) { |
1602 | 0 | s->ext.scts_len = 0; |
1603 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
1604 | 0 | return 0; |
1605 | 0 | } |
1606 | 0 | if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) { |
1607 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1608 | 0 | return 0; |
1609 | 0 | } |
1610 | 0 | } |
1611 | 18 | } else { |
1612 | 18 | ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0 |
1613 | 18 | ? ENDPOINT_CLIENT |
1614 | 18 | : ENDPOINT_BOTH; |
1615 | | |
1616 | | /* |
1617 | | * If we didn't ask for it then there must be a custom extension, |
1618 | | * otherwise this is unsolicited. |
1619 | | */ |
1620 | 18 | if (custom_ext_find(&s->cert->custext, role, |
1621 | 18 | TLSEXT_TYPE_signed_certificate_timestamp, |
1622 | 18 | NULL) |
1623 | 18 | == NULL) { |
1624 | 18 | SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1625 | 18 | return 0; |
1626 | 18 | } |
1627 | | |
1628 | 0 | if (!custom_ext_parse(s, context, |
1629 | 0 | TLSEXT_TYPE_signed_certificate_timestamp, |
1630 | 0 | PACKET_data(pkt), PACKET_remaining(pkt), |
1631 | 0 | x, chainidx)) { |
1632 | | /* SSLfatal already called */ |
1633 | 0 | return 0; |
1634 | 0 | } |
1635 | 0 | } |
1636 | | |
1637 | 0 | return 1; |
1638 | 18 | } |
1639 | | #endif |
1640 | | |
1641 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
1642 | | /* |
1643 | | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
1644 | | * elements of zero length are allowed and the set of elements must exactly |
1645 | | * fill the length of the block. Returns 1 on success or 0 on failure. |
1646 | | */ |
1647 | | static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt) |
1648 | 0 | { |
1649 | 0 | PACKET tmp_protocol; |
1650 | |
|
1651 | 0 | while (PACKET_remaining(pkt)) { |
1652 | 0 | if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol) |
1653 | 0 | || PACKET_remaining(&tmp_protocol) == 0) { |
1654 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1655 | 0 | return 0; |
1656 | 0 | } |
1657 | 0 | } |
1658 | | |
1659 | 0 | return 1; |
1660 | 0 | } |
1661 | | |
1662 | | int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1663 | | X509 *x, size_t chainidx) |
1664 | 0 | { |
1665 | 0 | unsigned char *selected; |
1666 | 0 | unsigned char selected_len; |
1667 | 0 | PACKET tmppkt; |
1668 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1669 | | |
1670 | | /* Check if we are in a renegotiation. If so ignore this extension */ |
1671 | 0 | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
1672 | 0 | return 1; |
1673 | | |
1674 | | /* We must have requested it. */ |
1675 | 0 | if (sctx->ext.npn_select_cb == NULL) { |
1676 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1677 | 0 | return 0; |
1678 | 0 | } |
1679 | | |
1680 | | /* The data must be valid */ |
1681 | 0 | tmppkt = *pkt; |
1682 | 0 | if (!ssl_next_proto_validate(s, &tmppkt)) { |
1683 | | /* SSLfatal() already called */ |
1684 | 0 | return 0; |
1685 | 0 | } |
1686 | 0 | if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_USER_SSL(s), |
1687 | 0 | &selected, &selected_len, |
1688 | 0 | PACKET_data(pkt), (unsigned int)PACKET_remaining(pkt), |
1689 | 0 | sctx->ext.npn_select_cb_arg) |
1690 | 0 | != SSL_TLSEXT_ERR_OK |
1691 | 0 | || selected_len == 0) { |
1692 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); |
1693 | 0 | return 0; |
1694 | 0 | } |
1695 | | |
1696 | | /* |
1697 | | * Could be non-NULL if server has sent multiple NPN extensions in |
1698 | | * a single Serverhello |
1699 | | */ |
1700 | 0 | OPENSSL_free(s->ext.npn); |
1701 | 0 | s->ext.npn = OPENSSL_malloc(selected_len); |
1702 | 0 | if (s->ext.npn == NULL) { |
1703 | 0 | s->ext.npn_len = 0; |
1704 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1705 | 0 | return 0; |
1706 | 0 | } |
1707 | | |
1708 | 0 | memcpy(s->ext.npn, selected, selected_len); |
1709 | 0 | s->ext.npn_len = selected_len; |
1710 | 0 | s->s3.npn_seen = 1; |
1711 | |
|
1712 | 0 | return 1; |
1713 | 0 | } |
1714 | | #endif |
1715 | | |
1716 | | int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1717 | | X509 *x, size_t chainidx) |
1718 | 20.8k | { |
1719 | 20.8k | size_t len; |
1720 | 20.8k | PACKET confpkt, protpkt; |
1721 | 20.8k | int valid = 0; |
1722 | | |
1723 | | /* We must have requested it. */ |
1724 | 20.8k | if (!s->s3.alpn_sent) { |
1725 | 0 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); |
1726 | 0 | return 0; |
1727 | 0 | } |
1728 | | /*- |
1729 | | * The extension data consists of: |
1730 | | * uint16 list_length |
1731 | | * uint8 proto_length; |
1732 | | * uint8 proto[proto_length]; |
1733 | | */ |
1734 | 20.8k | if (!PACKET_get_net_2_len(pkt, &len) |
1735 | 20.8k | || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len) |
1736 | 20.8k | || PACKET_remaining(pkt) != len) { |
1737 | 43 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1738 | 43 | return 0; |
1739 | 43 | } |
1740 | | |
1741 | | /* It must be a protocol that we sent */ |
1742 | 20.8k | if (!PACKET_buf_init(&confpkt, s->ext.alpn, s->ext.alpn_len)) { |
1743 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1744 | 0 | return 0; |
1745 | 0 | } |
1746 | 20.8k | while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) { |
1747 | 20.8k | if (PACKET_remaining(&protpkt) != len) |
1748 | 5 | continue; |
1749 | 20.8k | if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) { |
1750 | | /* Valid protocol found */ |
1751 | 20.7k | valid = 1; |
1752 | 20.7k | break; |
1753 | 20.7k | } |
1754 | 20.8k | } |
1755 | | |
1756 | 20.8k | if (!valid) { |
1757 | | /* The protocol sent from the server does not match one we advertised */ |
1758 | 55 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1759 | 55 | return 0; |
1760 | 55 | } |
1761 | | |
1762 | 20.7k | OPENSSL_free(s->s3.alpn_selected); |
1763 | 20.7k | s->s3.alpn_selected = OPENSSL_malloc(len); |
1764 | 20.7k | if (s->s3.alpn_selected == NULL) { |
1765 | 0 | s->s3.alpn_selected_len = 0; |
1766 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1767 | 0 | return 0; |
1768 | 0 | } |
1769 | 20.7k | if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) { |
1770 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1771 | 0 | return 0; |
1772 | 0 | } |
1773 | 20.7k | s->s3.alpn_selected_len = len; |
1774 | | |
1775 | 20.7k | if (s->session->ext.alpn_selected == NULL |
1776 | 0 | || s->session->ext.alpn_selected_len != len |
1777 | 0 | || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len) |
1778 | 20.7k | != 0) { |
1779 | | /* ALPN not consistent with the old session so cannot use early_data */ |
1780 | 20.7k | s->ext.early_data_ok = 0; |
1781 | 20.7k | } |
1782 | 20.7k | if (!s->hit) { |
1783 | | /* |
1784 | | * This is a new session and so alpn_selected should have been |
1785 | | * initialised to NULL. We should update it with the selected ALPN. |
1786 | | */ |
1787 | 20.7k | if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { |
1788 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1789 | 0 | return 0; |
1790 | 0 | } |
1791 | 20.7k | s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); |
1792 | 20.7k | if (s->session->ext.alpn_selected == NULL) { |
1793 | 0 | s->session->ext.alpn_selected_len = 0; |
1794 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1795 | 0 | return 0; |
1796 | 0 | } |
1797 | 20.7k | s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; |
1798 | 20.7k | } |
1799 | | |
1800 | 20.7k | return 1; |
1801 | 20.7k | } |
1802 | | |
1803 | | #ifndef OPENSSL_NO_SRTP |
1804 | | int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt, |
1805 | | unsigned int context, X509 *x, size_t chainidx) |
1806 | 0 | { |
1807 | 0 | unsigned int id, ct, mki; |
1808 | 0 | int i; |
1809 | 0 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt; |
1810 | 0 | SRTP_PROTECTION_PROFILE *prof; |
1811 | |
|
1812 | 0 | if (!PACKET_get_net_2(pkt, &ct) || ct != 2 |
1813 | 0 | || !PACKET_get_net_2(pkt, &id) |
1814 | 0 | || !PACKET_get_1(pkt, &mki) |
1815 | 0 | || PACKET_remaining(pkt) != 0) { |
1816 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
1817 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
1818 | 0 | return 0; |
1819 | 0 | } |
1820 | | |
1821 | 0 | if (mki != 0) { |
1822 | | /* Must be no MKI, since we never offer one */ |
1823 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE); |
1824 | 0 | return 0; |
1825 | 0 | } |
1826 | | |
1827 | | /* Throw an error if the server gave us an unsolicited extension */ |
1828 | 0 | clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s)); |
1829 | 0 | if (clnt == NULL) { |
1830 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES); |
1831 | 0 | return 0; |
1832 | 0 | } |
1833 | | |
1834 | | /* |
1835 | | * Check to see if the server gave us something we support (and |
1836 | | * presumably offered) |
1837 | | */ |
1838 | 0 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) { |
1839 | 0 | prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); |
1840 | |
|
1841 | 0 | if (prof->id == id) { |
1842 | 0 | s->srtp_profile = prof; |
1843 | 0 | return 1; |
1844 | 0 | } |
1845 | 0 | } |
1846 | | |
1847 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
1848 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
1849 | 0 | return 0; |
1850 | 0 | } |
1851 | | #endif |
1852 | | |
1853 | | int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1854 | | X509 *x, size_t chainidx) |
1855 | 4.23k | { |
1856 | | /* Ignore if inappropriate ciphersuite */ |
1857 | 4.23k | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) |
1858 | 4.23k | && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD |
1859 | 2.73k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4 |
1860 | 2.73k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT |
1861 | 2.73k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12 |
1862 | 2.73k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA |
1863 | 2.73k | && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK) |
1864 | 2.73k | s->ext.use_etm = 1; |
1865 | | |
1866 | 4.23k | return 1; |
1867 | 4.23k | } |
1868 | | |
1869 | | int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1870 | | X509 *x, size_t chainidx) |
1871 | 8.73k | { |
1872 | 8.73k | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
1873 | 0 | return 1; |
1874 | 8.73k | s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
1875 | 8.73k | if (!s->hit) |
1876 | 8.73k | s->session->flags |= SSL_SESS_FLAG_EXTMS; |
1877 | | |
1878 | 8.73k | return 1; |
1879 | 8.73k | } |
1880 | | |
1881 | | int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt, |
1882 | | unsigned int context, |
1883 | | X509 *x, size_t chainidx) |
1884 | 26.5k | { |
1885 | 26.5k | unsigned int version; |
1886 | | |
1887 | 26.5k | if (!PACKET_get_net_2(pkt, &version) |
1888 | 26.4k | || PACKET_remaining(pkt) != 0) { |
1889 | 92 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1890 | 92 | return 0; |
1891 | 92 | } |
1892 | | |
1893 | | /* |
1894 | | * The only protocol version we support which is valid in this extension in |
1895 | | * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else. |
1896 | | */ |
1897 | 26.4k | if (version != TLS1_3_VERSION) { |
1898 | 154 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1899 | 154 | SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
1900 | 154 | return 0; |
1901 | 154 | } |
1902 | | |
1903 | | /* We ignore this extension for HRRs except to sanity check it */ |
1904 | 26.3k | if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) |
1905 | 481 | return 1; |
1906 | | |
1907 | | /* We just set it here. We validate it in ssl_choose_client_version */ |
1908 | 25.8k | s->version = version; |
1909 | 25.8k | if (!ssl_set_record_protocol_version(s, version)) { |
1910 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1911 | 0 | return 0; |
1912 | 0 | } |
1913 | | |
1914 | 25.8k | return 1; |
1915 | 25.8k | } |
1916 | | |
1917 | | int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt, |
1918 | | unsigned int context, X509 *x, |
1919 | | size_t chainidx) |
1920 | 15.1k | { |
1921 | 15.1k | #ifndef OPENSSL_NO_TLS1_3 |
1922 | 15.1k | unsigned int group_id; |
1923 | 15.1k | PACKET encoded_pt; |
1924 | 15.1k | EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL; |
1925 | 15.1k | const TLS_GROUP_INFO *ginf = NULL; |
1926 | 15.1k | uint16_t valid_ks_id = 0; |
1927 | 15.1k | size_t i; |
1928 | | |
1929 | | /* Sanity check */ |
1930 | 15.1k | if (ckey == NULL || s->s3.peer_tmp != NULL) { |
1931 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1932 | 0 | return 0; |
1933 | 0 | } |
1934 | | |
1935 | | /* Which group ID does the server want -> group_id */ |
1936 | 15.1k | if (!PACKET_get_net_2(pkt, &group_id)) { |
1937 | 7 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1938 | 7 | return 0; |
1939 | 7 | } |
1940 | | |
1941 | 15.1k | if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) { |
1942 | 307 | const uint16_t *pgroups = NULL; |
1943 | 307 | size_t num_groups; |
1944 | | |
1945 | 307 | if (PACKET_remaining(pkt) != 0) { |
1946 | 7 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1947 | 7 | return 0; |
1948 | 7 | } |
1949 | | |
1950 | | /* |
1951 | | * It is an error if the HelloRetryRequest wants a key_share that we |
1952 | | * already sent in the first ClientHello |
1953 | | */ |
1954 | 894 | for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { |
1955 | 600 | if (s->s3.tmp.ks_group_id[i] == group_id) { |
1956 | 6 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
1957 | 6 | return 0; |
1958 | 6 | } |
1959 | 600 | } |
1960 | | |
1961 | | /* Validate the selected group is one we support */ |
1962 | 294 | tls1_get_supported_groups(s, &pgroups, &num_groups); |
1963 | 1.66k | for (i = 0; i < num_groups; i++) { |
1964 | 1.58k | if (group_id == pgroups[i]) |
1965 | 217 | break; |
1966 | 1.58k | } |
1967 | 294 | if (i >= num_groups |
1968 | 217 | || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED) |
1969 | 217 | || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION, |
1970 | 217 | NULL, NULL)) { |
1971 | 77 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
1972 | 77 | return 0; |
1973 | 77 | } |
1974 | | |
1975 | | /* Memorize which groupID the server wants */ |
1976 | 217 | s->s3.group_id = group_id; |
1977 | | |
1978 | | /* The initial keyshares are obsolete now, hence free memory */ |
1979 | 651 | for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { |
1980 | 434 | if (s->s3.tmp.ks_pkey[i] != NULL) { |
1981 | 434 | EVP_PKEY_free(s->s3.tmp.ks_pkey[i]); |
1982 | 434 | s->s3.tmp.ks_pkey[i] = NULL; |
1983 | 434 | } |
1984 | 434 | } |
1985 | 217 | s->s3.tmp.num_ks_pkey = 0; |
1986 | 217 | s->s3.tmp.pkey = NULL; |
1987 | | |
1988 | 217 | return 1; |
1989 | 294 | } |
1990 | | |
1991 | | /* |
1992 | | * check that the group requested by the server is one we've |
1993 | | * sent a key share for, and if so: memorize which one |
1994 | | */ |
1995 | 29.6k | for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) { |
1996 | 29.5k | if (s->s3.tmp.ks_group_id[i] == group_id) { |
1997 | 14.7k | valid_ks_id = group_id; |
1998 | 14.7k | ckey = s->s3.tmp.ks_pkey[i]; |
1999 | 14.7k | s->s3.group_id = group_id; |
2000 | 14.7k | s->s3.tmp.pkey = ckey; |
2001 | 14.7k | break; |
2002 | 14.7k | } |
2003 | 29.5k | } |
2004 | 14.7k | if (valid_ks_id == 0) { |
2005 | | /* |
2006 | | * This isn't for the group that we sent in the original |
2007 | | * key_share! |
2008 | | */ |
2009 | 93 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2010 | 93 | return 0; |
2011 | 93 | } |
2012 | | /* Retain this group in the SSL_SESSION */ |
2013 | 14.7k | if (!s->hit) { |
2014 | 14.7k | s->session->kex_group = group_id; |
2015 | 14.7k | } else if (group_id != s->session->kex_group) { |
2016 | | /* |
2017 | | * If this is a resumption but changed what group was used, we need |
2018 | | * to record the new group in the session, but the session is not |
2019 | | * a new session and could be in use by other threads. So, make |
2020 | | * a copy of the session to record the new information so that it's |
2021 | | * useful for any sessions resumed from tickets issued on this |
2022 | | * connection. |
2023 | | */ |
2024 | 0 | SSL_SESSION *new_sess; |
2025 | |
|
2026 | 0 | if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) { |
2027 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
2028 | 0 | return 0; |
2029 | 0 | } |
2030 | 0 | SSL_SESSION_free(s->session); |
2031 | 0 | s->session = new_sess; |
2032 | 0 | s->session->kex_group = group_id; |
2033 | 0 | } |
2034 | | |
2035 | 14.7k | if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
2036 | 14.7k | group_id)) |
2037 | 14.7k | == NULL) { |
2038 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2039 | 0 | return 0; |
2040 | 0 | } |
2041 | | |
2042 | 14.7k | if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt) |
2043 | 14.6k | || PACKET_remaining(&encoded_pt) == 0) { |
2044 | 99 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2045 | 99 | return 0; |
2046 | 99 | } |
2047 | | |
2048 | 14.6k | if (!ginf->is_kem) { |
2049 | | /* Regular KEX */ |
2050 | 14.6k | skey = EVP_PKEY_new(); |
2051 | 14.6k | if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) { |
2052 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); |
2053 | 0 | EVP_PKEY_free(skey); |
2054 | 0 | return 0; |
2055 | 0 | } |
2056 | | |
2057 | 14.6k | if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt), |
2058 | 14.6k | PACKET_remaining(&encoded_pt)) |
2059 | 14.6k | <= 0) { |
2060 | 31 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
2061 | 31 | EVP_PKEY_free(skey); |
2062 | 31 | return 0; |
2063 | 31 | } |
2064 | | |
2065 | 14.5k | if (ssl_derive(s, ckey, skey, 1) == 0) { |
2066 | | /* SSLfatal() already called */ |
2067 | 9 | EVP_PKEY_free(skey); |
2068 | 9 | return 0; |
2069 | 9 | } |
2070 | 14.5k | s->s3.peer_tmp = skey; |
2071 | 14.5k | } else { |
2072 | | /* KEM Mode */ |
2073 | 0 | const unsigned char *ct = PACKET_data(&encoded_pt); |
2074 | 0 | size_t ctlen = PACKET_remaining(&encoded_pt); |
2075 | |
|
2076 | 0 | if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) { |
2077 | | /* SSLfatal() already called */ |
2078 | 0 | return 0; |
2079 | 0 | } |
2080 | 0 | } |
2081 | 14.5k | s->s3.did_kex = 1; |
2082 | 14.5k | #endif |
2083 | | |
2084 | 14.5k | return 1; |
2085 | 14.6k | } |
2086 | | |
2087 | | int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
2088 | | X509 *x, size_t chainidx) |
2089 | 140 | { |
2090 | 140 | PACKET cookie; |
2091 | | |
2092 | 140 | if (!PACKET_as_length_prefixed_2(pkt, &cookie) |
2093 | 52 | || !PACKET_memdup(&cookie, &s->ext.tls13_cookie, |
2094 | 88 | &s->ext.tls13_cookie_len)) { |
2095 | 88 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2096 | 88 | return 0; |
2097 | 88 | } |
2098 | | |
2099 | 52 | return 1; |
2100 | 140 | } |
2101 | | |
2102 | | int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt, |
2103 | | unsigned int context, |
2104 | | X509 *x, size_t chainidx) |
2105 | 0 | { |
2106 | 0 | if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { |
2107 | 0 | unsigned long max_early_data; |
2108 | |
|
2109 | 0 | if (!PACKET_get_net_4(pkt, &max_early_data) |
2110 | 0 | || PACKET_remaining(pkt) != 0) { |
2111 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA); |
2112 | 0 | return 0; |
2113 | 0 | } |
2114 | | |
2115 | 0 | s->session->ext.max_early_data = max_early_data; |
2116 | |
|
2117 | 0 | if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) { |
2118 | | /* |
2119 | | * QUIC allows missing max_early_data, or a max_early_data value |
2120 | | * of 0xffffffff. Missing max_early_data is stored in the session |
2121 | | * as 0. This is indistinguishable in OpenSSL from a present |
2122 | | * max_early_data value that was 0. In order that later checks for |
2123 | | * invalid max_early_data correctly treat as an error the case where |
2124 | | * max_early_data is present and it is 0, we store any invalid |
2125 | | * value in the same (non-zero) way. Otherwise we would have to |
2126 | | * introduce a new flag just for this. |
2127 | | */ |
2128 | 0 | s->session->ext.max_early_data = 1; |
2129 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA); |
2130 | 0 | return 0; |
2131 | 0 | } |
2132 | | |
2133 | 0 | return 1; |
2134 | 0 | } |
2135 | | |
2136 | 0 | if (PACKET_remaining(pkt) != 0) { |
2137 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2138 | 0 | return 0; |
2139 | 0 | } |
2140 | | |
2141 | 0 | if (!s->ext.early_data_ok |
2142 | 0 | || !s->hit) { |
2143 | | /* |
2144 | | * If we get here then we didn't send early data, or we didn't resume |
2145 | | * using the first identity, or the SNI/ALPN is not consistent so the |
2146 | | * server should not be accepting it. |
2147 | | */ |
2148 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
2149 | 0 | return 0; |
2150 | 0 | } |
2151 | | |
2152 | 0 | s->ext.early_data = SSL_EARLY_DATA_ACCEPTED; |
2153 | |
|
2154 | 0 | return 1; |
2155 | 0 | } |
2156 | | |
2157 | | int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt, |
2158 | | unsigned int context, X509 *x, |
2159 | | size_t chainidx) |
2160 | 0 | { |
2161 | 0 | #ifndef OPENSSL_NO_TLS1_3 |
2162 | 0 | unsigned int identity; |
2163 | |
|
2164 | 0 | if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) { |
2165 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
2166 | 0 | return 0; |
2167 | 0 | } |
2168 | | |
2169 | 0 | if (identity >= (unsigned int)s->ext.tick_identity) { |
2170 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY); |
2171 | 0 | return 0; |
2172 | 0 | } |
2173 | | |
2174 | | /* |
2175 | | * Session resumption tickets are always sent before PSK tickets. If the |
2176 | | * ticket index is 0 then it must be for a session resumption ticket if we |
2177 | | * sent two tickets, or if we didn't send a PSK ticket. |
2178 | | */ |
2179 | 0 | if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) { |
2180 | 0 | s->hit = 1; |
2181 | 0 | SSL_SESSION_free(s->psksession); |
2182 | 0 | s->psksession = NULL; |
2183 | 0 | return 1; |
2184 | 0 | } |
2185 | | |
2186 | 0 | if (s->psksession == NULL) { |
2187 | | /* Should never happen */ |
2188 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2189 | 0 | return 0; |
2190 | 0 | } |
2191 | | |
2192 | | /* |
2193 | | * If we used the external PSK for sending early_data then s->early_secret |
2194 | | * is already set up, so don't overwrite it. Otherwise we copy the |
2195 | | * early_secret across that we generated earlier. |
2196 | | */ |
2197 | 0 | if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY |
2198 | 0 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) |
2199 | 0 | || s->session->ext.max_early_data > 0 |
2200 | 0 | || s->psksession->ext.max_early_data == 0) |
2201 | 0 | memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE); |
2202 | |
|
2203 | 0 | SSL_SESSION_free(s->session); |
2204 | 0 | s->session = s->psksession; |
2205 | 0 | s->psksession = NULL; |
2206 | 0 | s->hit = 1; |
2207 | | /* Early data is only allowed if we used the first ticket */ |
2208 | 0 | if (identity != 0) |
2209 | 0 | s->ext.early_data_ok = 0; |
2210 | 0 | #endif |
2211 | |
|
2212 | 0 | return 1; |
2213 | 0 | } |
2214 | | |
2215 | | EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt, |
2216 | | unsigned int context, |
2217 | | X509 *x, size_t chainidx) |
2218 | 113k | { |
2219 | 113k | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2220 | 113k | if (sc->client_cert_type == NULL) |
2221 | 113k | return EXT_RETURN_NOT_SENT; |
2222 | | |
2223 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type) |
2224 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2225 | 0 | || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len) |
2226 | 0 | || !WPACKET_close(pkt)) { |
2227 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2228 | 0 | return EXT_RETURN_FAIL; |
2229 | 0 | } |
2230 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD; |
2231 | 0 | return EXT_RETURN_SENT; |
2232 | 0 | } |
2233 | | |
2234 | | int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt, |
2235 | | unsigned int context, |
2236 | | X509 *x, size_t chainidx) |
2237 | 0 | { |
2238 | 0 | unsigned int type; |
2239 | |
|
2240 | 0 | if (PACKET_remaining(pkt) != 1) { |
2241 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2242 | 0 | return 0; |
2243 | 0 | } |
2244 | 0 | if (!PACKET_get_1(pkt, &type)) { |
2245 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2246 | 0 | return 0; |
2247 | 0 | } |
2248 | | /* We did not send/ask for this */ |
2249 | 0 | if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) { |
2250 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2251 | 0 | return 0; |
2252 | 0 | } |
2253 | | /* We don't have this enabled */ |
2254 | 0 | if (sc->client_cert_type == NULL) { |
2255 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2256 | 0 | return 0; |
2257 | 0 | } |
2258 | | /* Given back a value we didn't configure */ |
2259 | 0 | if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) { |
2260 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE); |
2261 | 0 | return 0; |
2262 | 0 | } |
2263 | 0 | sc->ext.client_cert_type = type; |
2264 | 0 | return 1; |
2265 | 0 | } |
2266 | | |
2267 | | EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt, |
2268 | | unsigned int context, |
2269 | | X509 *x, size_t chainidx) |
2270 | 113k | { |
2271 | 113k | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2272 | 113k | if (sc->server_cert_type == NULL) |
2273 | 113k | return EXT_RETURN_NOT_SENT; |
2274 | | |
2275 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type) |
2276 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2277 | 0 | || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len) |
2278 | 0 | || !WPACKET_close(pkt)) { |
2279 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2280 | 0 | return EXT_RETURN_FAIL; |
2281 | 0 | } |
2282 | 0 | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD; |
2283 | 0 | return EXT_RETURN_SENT; |
2284 | 0 | } |
2285 | | |
2286 | | int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt, |
2287 | | unsigned int context, |
2288 | | X509 *x, size_t chainidx) |
2289 | 0 | { |
2290 | 0 | unsigned int type; |
2291 | |
|
2292 | 0 | if (PACKET_remaining(pkt) != 1) { |
2293 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2294 | 0 | return 0; |
2295 | 0 | } |
2296 | 0 | if (!PACKET_get_1(pkt, &type)) { |
2297 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2298 | 0 | return 0; |
2299 | 0 | } |
2300 | | /* We did not send/ask for this */ |
2301 | 0 | if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) { |
2302 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2303 | 0 | return 0; |
2304 | 0 | } |
2305 | | /* We don't have this enabled */ |
2306 | 0 | if (sc->server_cert_type == NULL) { |
2307 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2308 | 0 | return 0; |
2309 | 0 | } |
2310 | | /* Given back a value we didn't configure */ |
2311 | 0 | if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) { |
2312 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE); |
2313 | 0 | return 0; |
2314 | 0 | } |
2315 | 0 | sc->ext.server_cert_type = type; |
2316 | 0 | return 1; |
2317 | 0 | } |