2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/ocsp.h>
11 #include "../ssl_local.h"
12 #include "internal/cryptlib.h"
13 #include "statem_local.h"
15 EXT_RETURN
tls_construct_ctos_renegotiate(SSL
*s
, WPACKET
*pkt
,
16 unsigned int context
, X509
*x
,
19 /* Add RI if renegotiating */
21 return EXT_RETURN_NOT_SENT
;
23 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_renegotiate
)
24 || !WPACKET_start_sub_packet_u16(pkt
)
25 || !WPACKET_sub_memcpy_u8(pkt
, s
->s3
.previous_client_finished
,
26 s
->s3
.previous_client_finished_len
)
27 || !WPACKET_close(pkt
)) {
28 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
29 return EXT_RETURN_FAIL
;
32 return EXT_RETURN_SENT
;
35 EXT_RETURN
tls_construct_ctos_server_name(SSL
*s
, WPACKET
*pkt
,
36 unsigned int context
, X509
*x
,
39 if (s
->ext
.hostname
== NULL
)
40 return EXT_RETURN_NOT_SENT
;
42 /* Add TLS extension servername to the Client Hello message */
43 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_server_name
)
44 /* Sub-packet for server_name extension */
45 || !WPACKET_start_sub_packet_u16(pkt
)
46 /* Sub-packet for servername list (always 1 hostname)*/
47 || !WPACKET_start_sub_packet_u16(pkt
)
48 || !WPACKET_put_bytes_u8(pkt
, TLSEXT_NAMETYPE_host_name
)
49 || !WPACKET_sub_memcpy_u16(pkt
, s
->ext
.hostname
,
50 strlen(s
->ext
.hostname
))
51 || !WPACKET_close(pkt
)
52 || !WPACKET_close(pkt
)) {
53 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
54 return EXT_RETURN_FAIL
;
57 return EXT_RETURN_SENT
;
60 /* Push a Max Fragment Len extension into ClientHello */
61 EXT_RETURN
tls_construct_ctos_maxfragmentlen(SSL
*s
, WPACKET
*pkt
,
62 unsigned int context
, X509
*x
,
65 if (s
->ext
.max_fragment_len_mode
== TLSEXT_max_fragment_length_DISABLED
)
66 return EXT_RETURN_NOT_SENT
;
68 /* Add Max Fragment Length extension if client enabled it. */
70 * 4 bytes for this extension type and extension length
71 * 1 byte for the Max Fragment Length code value.
73 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_max_fragment_length
)
74 /* Sub-packet for Max Fragment Length extension (1 byte) */
75 || !WPACKET_start_sub_packet_u16(pkt
)
76 || !WPACKET_put_bytes_u8(pkt
, s
->ext
.max_fragment_len_mode
)
77 || !WPACKET_close(pkt
)) {
78 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
79 return EXT_RETURN_FAIL
;
82 return EXT_RETURN_SENT
;
85 #ifndef OPENSSL_NO_SRP
86 EXT_RETURN
tls_construct_ctos_srp(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
87 X509
*x
, size_t chainidx
)
89 /* Add SRP username if there is one */
90 if (s
->srp_ctx
.login
== NULL
)
91 return EXT_RETURN_NOT_SENT
;
93 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_srp
)
94 /* Sub-packet for SRP extension */
95 || !WPACKET_start_sub_packet_u16(pkt
)
96 || !WPACKET_start_sub_packet_u8(pkt
)
97 /* login must not be zero...internal error if so */
98 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
99 || !WPACKET_memcpy(pkt
, s
->srp_ctx
.login
,
100 strlen(s
->srp_ctx
.login
))
101 || !WPACKET_close(pkt
)
102 || !WPACKET_close(pkt
)) {
103 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
104 return EXT_RETURN_FAIL
;
107 return EXT_RETURN_SENT
;
111 static int use_ecc(SSL
*s
, int min_version
, int max_version
)
114 unsigned long alg_k
, alg_a
;
115 STACK_OF(SSL_CIPHER
) *cipher_stack
= NULL
;
116 const uint16_t *pgroups
= NULL
;
117 size_t num_groups
, j
;
119 /* See if we support any ECC ciphersuites */
120 if (s
->version
== SSL3_VERSION
)
123 cipher_stack
= SSL_get1_supported_ciphers(s
);
124 end
= sk_SSL_CIPHER_num(cipher_stack
);
125 for (i
= 0; i
< end
; i
++) {
126 const SSL_CIPHER
*c
= sk_SSL_CIPHER_value(cipher_stack
, i
);
128 alg_k
= c
->algorithm_mkey
;
129 alg_a
= c
->algorithm_auth
;
130 if ((alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
))
131 || (alg_a
& SSL_aECDSA
)
132 || c
->min_tls
>= TLS1_3_VERSION
) {
137 sk_SSL_CIPHER_free(cipher_stack
);
141 /* Check we have at least one EC supported group */
142 tls1_get_supported_groups(s
, &pgroups
, &num_groups
);
143 for (j
= 0; j
< num_groups
; j
++) {
144 uint16_t ctmp
= pgroups
[j
];
146 if (tls_valid_group(s
, ctmp
, min_version
, max_version
, 1, NULL
)
147 && tls_group_allowed(s
, ctmp
, SSL_SECOP_CURVE_SUPPORTED
))
154 EXT_RETURN
tls_construct_ctos_ec_pt_formats(SSL
*s
, WPACKET
*pkt
,
155 unsigned int context
, X509
*x
,
158 const unsigned char *pformats
;
160 int reason
, min_version
, max_version
;
162 reason
= ssl_get_min_max_version(s
, &min_version
, &max_version
, NULL
);
164 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, reason
);
165 return EXT_RETURN_FAIL
;
167 if (!use_ecc(s
, min_version
, max_version
))
168 return EXT_RETURN_NOT_SENT
;
170 /* Add TLS extension ECPointFormats to the ClientHello message */
171 tls1_get_formatlist(s
, &pformats
, &num_formats
);
173 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_ec_point_formats
)
174 /* Sub-packet for formats extension */
175 || !WPACKET_start_sub_packet_u16(pkt
)
176 || !WPACKET_sub_memcpy_u8(pkt
, pformats
, num_formats
)
177 || !WPACKET_close(pkt
)) {
178 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
179 return EXT_RETURN_FAIL
;
182 return EXT_RETURN_SENT
;
185 EXT_RETURN
tls_construct_ctos_supported_groups(SSL
*s
, WPACKET
*pkt
,
186 unsigned int context
, X509
*x
,
189 const uint16_t *pgroups
= NULL
;
190 size_t num_groups
= 0, i
, tls13added
= 0, added
= 0;
191 int min_version
, max_version
, reason
;
193 reason
= ssl_get_min_max_version(s
, &min_version
, &max_version
, NULL
);
195 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, reason
);
196 return EXT_RETURN_FAIL
;
200 * We only support EC groups in TLSv1.2 or below, and in DTLS. Therefore
201 * if we don't have EC support then we don't send this extension.
203 if (!use_ecc(s
, min_version
, max_version
)
204 && (SSL_IS_DTLS(s
) || max_version
< TLS1_3_VERSION
))
205 return EXT_RETURN_NOT_SENT
;
208 * Add TLS extension supported_groups to the ClientHello message
210 tls1_get_supported_groups(s
, &pgroups
, &num_groups
);
212 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_supported_groups
)
213 /* Sub-packet for supported_groups extension */
214 || !WPACKET_start_sub_packet_u16(pkt
)
215 || !WPACKET_start_sub_packet_u16(pkt
)
216 || !WPACKET_set_flags(pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)) {
217 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
218 return EXT_RETURN_FAIL
;
220 /* Copy group ID if supported */
221 for (i
= 0; i
< num_groups
; i
++) {
222 uint16_t ctmp
= pgroups
[i
];
225 if (tls_valid_group(s
, ctmp
, min_version
, max_version
, 0, &okfortls13
)
226 && tls_group_allowed(s
, ctmp
, SSL_SECOP_CURVE_SUPPORTED
)) {
227 if (!WPACKET_put_bytes_u16(pkt
, ctmp
)) {
228 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
229 return EXT_RETURN_FAIL
;
231 if (okfortls13
&& max_version
== TLS1_3_VERSION
)
236 if (!WPACKET_close(pkt
) || !WPACKET_close(pkt
)) {
238 SSLfatal_data(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_SUITABLE_GROUPS
,
239 "No groups enabled for max supported SSL/TLS version");
241 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
242 return EXT_RETURN_FAIL
;
245 if (tls13added
== 0 && max_version
== TLS1_3_VERSION
) {
246 SSLfatal_data(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_SUITABLE_GROUPS
,
247 "No groups enabled for max supported SSL/TLS version");
248 return EXT_RETURN_FAIL
;
251 return EXT_RETURN_SENT
;
254 EXT_RETURN
tls_construct_ctos_session_ticket(SSL
*s
, WPACKET
*pkt
,
255 unsigned int context
, X509
*x
,
260 if (!tls_use_ticket(s
))
261 return EXT_RETURN_NOT_SENT
;
263 if (!s
->new_session
&& s
->session
!= NULL
264 && s
->session
->ext
.tick
!= NULL
265 && s
->session
->ssl_version
!= TLS1_3_VERSION
) {
266 ticklen
= s
->session
->ext
.ticklen
;
267 } else if (s
->session
&& s
->ext
.session_ticket
!= NULL
268 && s
->ext
.session_ticket
->data
!= NULL
) {
269 ticklen
= s
->ext
.session_ticket
->length
;
270 s
->session
->ext
.tick
= OPENSSL_malloc(ticklen
);
271 if (s
->session
->ext
.tick
== NULL
) {
272 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
273 return EXT_RETURN_FAIL
;
275 memcpy(s
->session
->ext
.tick
,
276 s
->ext
.session_ticket
->data
, ticklen
);
277 s
->session
->ext
.ticklen
= ticklen
;
282 if (ticklen
== 0 && s
->ext
.session_ticket
!= NULL
&&
283 s
->ext
.session_ticket
->data
== NULL
)
284 return EXT_RETURN_NOT_SENT
;
286 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_session_ticket
)
287 || !WPACKET_sub_memcpy_u16(pkt
, s
->session
->ext
.tick
, ticklen
)) {
288 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
289 return EXT_RETURN_FAIL
;
292 return EXT_RETURN_SENT
;
295 EXT_RETURN
tls_construct_ctos_sig_algs(SSL
*s
, WPACKET
*pkt
,
296 unsigned int context
, X509
*x
,
300 const uint16_t *salg
;
302 if (!SSL_CLIENT_USE_SIGALGS(s
))
303 return EXT_RETURN_NOT_SENT
;
305 salglen
= tls12_get_psigalgs(s
, 1, &salg
);
306 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_signature_algorithms
)
307 /* Sub-packet for sig-algs extension */
308 || !WPACKET_start_sub_packet_u16(pkt
)
309 /* Sub-packet for the actual list */
310 || !WPACKET_start_sub_packet_u16(pkt
)
311 || !tls12_copy_sigalgs(s
, pkt
, salg
, salglen
)
312 || !WPACKET_close(pkt
)
313 || !WPACKET_close(pkt
)) {
314 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
315 return EXT_RETURN_FAIL
;
318 return EXT_RETURN_SENT
;
321 #ifndef OPENSSL_NO_OCSP
322 EXT_RETURN
tls_construct_ctos_status_request(SSL
*s
, WPACKET
*pkt
,
323 unsigned int context
, X509
*x
,
328 /* This extension isn't defined for client Certificates */
330 return EXT_RETURN_NOT_SENT
;
332 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_ocsp
)
333 return EXT_RETURN_NOT_SENT
;
335 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_status_request
)
336 /* Sub-packet for status request extension */
337 || !WPACKET_start_sub_packet_u16(pkt
)
338 || !WPACKET_put_bytes_u8(pkt
, TLSEXT_STATUSTYPE_ocsp
)
339 /* Sub-packet for the ids */
340 || !WPACKET_start_sub_packet_u16(pkt
)) {
341 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
342 return EXT_RETURN_FAIL
;
344 for (i
= 0; i
< sk_OCSP_RESPID_num(s
->ext
.ocsp
.ids
); i
++) {
345 unsigned char *idbytes
;
346 OCSP_RESPID
*id
= sk_OCSP_RESPID_value(s
->ext
.ocsp
.ids
, i
);
347 int idlen
= i2d_OCSP_RESPID(id
, NULL
);
350 /* Sub-packet for an individual id */
351 || !WPACKET_sub_allocate_bytes_u16(pkt
, idlen
, &idbytes
)
352 || i2d_OCSP_RESPID(id
, &idbytes
) != idlen
) {
353 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
354 return EXT_RETURN_FAIL
;
357 if (!WPACKET_close(pkt
)
358 || !WPACKET_start_sub_packet_u16(pkt
)) {
359 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
360 return EXT_RETURN_FAIL
;
362 if (s
->ext
.ocsp
.exts
) {
363 unsigned char *extbytes
;
364 int extlen
= i2d_X509_EXTENSIONS(s
->ext
.ocsp
.exts
, NULL
);
367 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
368 return EXT_RETURN_FAIL
;
370 if (!WPACKET_allocate_bytes(pkt
, extlen
, &extbytes
)
371 || i2d_X509_EXTENSIONS(s
->ext
.ocsp
.exts
, &extbytes
)
373 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
374 return EXT_RETURN_FAIL
;
377 if (!WPACKET_close(pkt
) || !WPACKET_close(pkt
)) {
378 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
379 return EXT_RETURN_FAIL
;
382 return EXT_RETURN_SENT
;
386 #ifndef OPENSSL_NO_NEXTPROTONEG
387 EXT_RETURN
tls_construct_ctos_npn(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
388 X509
*x
, size_t chainidx
)
390 if (s
->ctx
->ext
.npn_select_cb
== NULL
|| !SSL_IS_FIRST_HANDSHAKE(s
))
391 return EXT_RETURN_NOT_SENT
;
394 * The client advertises an empty extension to indicate its support
395 * for Next Protocol Negotiation
397 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_next_proto_neg
)
398 || !WPACKET_put_bytes_u16(pkt
, 0)) {
399 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
400 return EXT_RETURN_FAIL
;
403 return EXT_RETURN_SENT
;
407 EXT_RETURN
tls_construct_ctos_alpn(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
408 X509
*x
, size_t chainidx
)
412 if (s
->ext
.alpn
== NULL
|| !SSL_IS_FIRST_HANDSHAKE(s
))
413 return EXT_RETURN_NOT_SENT
;
415 if (!WPACKET_put_bytes_u16(pkt
,
416 TLSEXT_TYPE_application_layer_protocol_negotiation
)
417 /* Sub-packet ALPN extension */
418 || !WPACKET_start_sub_packet_u16(pkt
)
419 || !WPACKET_sub_memcpy_u16(pkt
, s
->ext
.alpn
, s
->ext
.alpn_len
)
420 || !WPACKET_close(pkt
)) {
421 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
422 return EXT_RETURN_FAIL
;
426 return EXT_RETURN_SENT
;
430 #ifndef OPENSSL_NO_SRTP
431 EXT_RETURN
tls_construct_ctos_use_srtp(SSL
*s
, WPACKET
*pkt
,
432 unsigned int context
, X509
*x
,
435 STACK_OF(SRTP_PROTECTION_PROFILE
) *clnt
= SSL_get_srtp_profiles(s
);
439 return EXT_RETURN_NOT_SENT
;
441 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_use_srtp
)
442 /* Sub-packet for SRTP extension */
443 || !WPACKET_start_sub_packet_u16(pkt
)
444 /* Sub-packet for the protection profile list */
445 || !WPACKET_start_sub_packet_u16(pkt
)) {
446 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
447 return EXT_RETURN_FAIL
;
450 end
= sk_SRTP_PROTECTION_PROFILE_num(clnt
);
451 for (i
= 0; i
< end
; i
++) {
452 const SRTP_PROTECTION_PROFILE
*prof
=
453 sk_SRTP_PROTECTION_PROFILE_value(clnt
, i
);
455 if (prof
== NULL
|| !WPACKET_put_bytes_u16(pkt
, prof
->id
)) {
456 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
457 return EXT_RETURN_FAIL
;
460 if (!WPACKET_close(pkt
)
461 /* Add an empty use_mki value */
462 || !WPACKET_put_bytes_u8(pkt
, 0)
463 || !WPACKET_close(pkt
)) {
464 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
465 return EXT_RETURN_FAIL
;
468 return EXT_RETURN_SENT
;
472 EXT_RETURN
tls_construct_ctos_etm(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
473 X509
*x
, size_t chainidx
)
475 if (s
->options
& SSL_OP_NO_ENCRYPT_THEN_MAC
)
476 return EXT_RETURN_NOT_SENT
;
478 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_encrypt_then_mac
)
479 || !WPACKET_put_bytes_u16(pkt
, 0)) {
480 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
481 return EXT_RETURN_FAIL
;
484 return EXT_RETURN_SENT
;
487 #ifndef OPENSSL_NO_CT
488 EXT_RETURN
tls_construct_ctos_sct(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
489 X509
*x
, size_t chainidx
)
491 if (s
->ct_validation_callback
== NULL
)
492 return EXT_RETURN_NOT_SENT
;
494 /* Not defined for client Certificates */
496 return EXT_RETURN_NOT_SENT
;
498 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_signed_certificate_timestamp
)
499 || !WPACKET_put_bytes_u16(pkt
, 0)) {
500 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
501 return EXT_RETURN_FAIL
;
504 return EXT_RETURN_SENT
;
508 EXT_RETURN
tls_construct_ctos_ems(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
509 X509
*x
, size_t chainidx
)
511 if (s
->options
& SSL_OP_NO_EXTENDED_MASTER_SECRET
)
512 return EXT_RETURN_NOT_SENT
;
514 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_extended_master_secret
)
515 || !WPACKET_put_bytes_u16(pkt
, 0)) {
516 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
517 return EXT_RETURN_FAIL
;
520 return EXT_RETURN_SENT
;
523 EXT_RETURN
tls_construct_ctos_supported_versions(SSL
*s
, WPACKET
*pkt
,
524 unsigned int context
, X509
*x
,
527 int currv
, min_version
, max_version
, reason
;
529 reason
= ssl_get_min_max_version(s
, &min_version
, &max_version
, NULL
);
531 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, reason
);
532 return EXT_RETURN_FAIL
;
536 * Don't include this if we can't negotiate TLSv1.3. We can do a straight
537 * comparison here because we will never be called in DTLS.
539 if (max_version
< TLS1_3_VERSION
)
540 return EXT_RETURN_NOT_SENT
;
542 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_supported_versions
)
543 || !WPACKET_start_sub_packet_u16(pkt
)
544 || !WPACKET_start_sub_packet_u8(pkt
)) {
545 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
546 return EXT_RETURN_FAIL
;
549 for (currv
= max_version
; currv
>= min_version
; currv
--) {
550 if (!WPACKET_put_bytes_u16(pkt
, currv
)) {
551 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
552 return EXT_RETURN_FAIL
;
555 if (!WPACKET_close(pkt
) || !WPACKET_close(pkt
)) {
556 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
557 return EXT_RETURN_FAIL
;
560 return EXT_RETURN_SENT
;
564 * Construct a psk_kex_modes extension.
566 EXT_RETURN
tls_construct_ctos_psk_kex_modes(SSL
*s
, WPACKET
*pkt
,
567 unsigned int context
, X509
*x
,
570 #ifndef OPENSSL_NO_TLS1_3
571 int nodhe
= s
->options
& SSL_OP_ALLOW_NO_DHE_KEX
;
573 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_psk_kex_modes
)
574 || !WPACKET_start_sub_packet_u16(pkt
)
575 || !WPACKET_start_sub_packet_u8(pkt
)
576 || !WPACKET_put_bytes_u8(pkt
, TLSEXT_KEX_MODE_KE_DHE
)
577 || (nodhe
&& !WPACKET_put_bytes_u8(pkt
, TLSEXT_KEX_MODE_KE
))
578 || !WPACKET_close(pkt
)
579 || !WPACKET_close(pkt
)) {
580 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
581 return EXT_RETURN_FAIL
;
584 s
->ext
.psk_kex_mode
= TLSEXT_KEX_MODE_FLAG_KE_DHE
;
586 s
->ext
.psk_kex_mode
|= TLSEXT_KEX_MODE_FLAG_KE
;
589 return EXT_RETURN_SENT
;
592 #ifndef OPENSSL_NO_TLS1_3
593 static int add_key_share(SSL
*s
, WPACKET
*pkt
, unsigned int curve_id
)
595 unsigned char *encoded_point
= NULL
;
596 EVP_PKEY
*key_share_key
= NULL
;
599 if (s
->s3
.tmp
.pkey
!= NULL
) {
600 if (!ossl_assert(s
->hello_retry_request
== SSL_HRR_PENDING
)) {
601 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
605 * Could happen if we got an HRR that wasn't requesting a new key_share
607 key_share_key
= s
->s3
.tmp
.pkey
;
609 key_share_key
= ssl_generate_pkey_group(s
, curve_id
);
610 if (key_share_key
== NULL
) {
611 /* SSLfatal() already called */
616 /* Encode the public key. */
617 encodedlen
= EVP_PKEY_get1_encoded_public_key(key_share_key
,
619 if (encodedlen
== 0) {
620 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EC_LIB
);
624 /* Create KeyShareEntry */
625 if (!WPACKET_put_bytes_u16(pkt
, curve_id
)
626 || !WPACKET_sub_memcpy_u16(pkt
, encoded_point
, encodedlen
)) {
627 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
632 * TODO(TLS1.3): When changing to send more than one key_share we're
633 * going to need to be able to save more than one EVP_PKEY. For now
634 * we reuse the existing tmp.pkey
636 s
->s3
.tmp
.pkey
= key_share_key
;
637 s
->s3
.group_id
= curve_id
;
638 OPENSSL_free(encoded_point
);
642 if (s
->s3
.tmp
.pkey
== NULL
)
643 EVP_PKEY_free(key_share_key
);
644 OPENSSL_free(encoded_point
);
649 EXT_RETURN
tls_construct_ctos_key_share(SSL
*s
, WPACKET
*pkt
,
650 unsigned int context
, X509
*x
,
653 #ifndef OPENSSL_NO_TLS1_3
654 size_t i
, num_groups
= 0;
655 const uint16_t *pgroups
= NULL
;
656 uint16_t curve_id
= 0;
658 /* key_share extension */
659 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_key_share
)
660 /* Extension data sub-packet */
661 || !WPACKET_start_sub_packet_u16(pkt
)
662 /* KeyShare list sub-packet */
663 || !WPACKET_start_sub_packet_u16(pkt
)) {
664 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
665 return EXT_RETURN_FAIL
;
668 tls1_get_supported_groups(s
, &pgroups
, &num_groups
);
671 * TODO(TLS1.3): Make the number of key_shares sent configurable. For
674 if (s
->s3
.group_id
!= 0) {
675 curve_id
= s
->s3
.group_id
;
677 for (i
= 0; i
< num_groups
; i
++) {
679 if (!tls_group_allowed(s
, pgroups
[i
], SSL_SECOP_CURVE_SUPPORTED
))
682 curve_id
= pgroups
[i
];
688 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_SUITABLE_KEY_SHARE
);
689 return EXT_RETURN_FAIL
;
692 if (!add_key_share(s
, pkt
, curve_id
)) {
693 /* SSLfatal() already called */
694 return EXT_RETURN_FAIL
;
697 if (!WPACKET_close(pkt
) || !WPACKET_close(pkt
)) {
698 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
699 return EXT_RETURN_FAIL
;
701 return EXT_RETURN_SENT
;
703 return EXT_RETURN_NOT_SENT
;
707 EXT_RETURN
tls_construct_ctos_cookie(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
708 X509
*x
, size_t chainidx
)
710 EXT_RETURN ret
= EXT_RETURN_FAIL
;
712 /* Should only be set if we've had an HRR */
713 if (s
->ext
.tls13_cookie_len
== 0)
714 return EXT_RETURN_NOT_SENT
;
716 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_cookie
)
717 /* Extension data sub-packet */
718 || !WPACKET_start_sub_packet_u16(pkt
)
719 || !WPACKET_sub_memcpy_u16(pkt
, s
->ext
.tls13_cookie
,
720 s
->ext
.tls13_cookie_len
)
721 || !WPACKET_close(pkt
)) {
722 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
726 ret
= EXT_RETURN_SENT
;
728 OPENSSL_free(s
->ext
.tls13_cookie
);
729 s
->ext
.tls13_cookie
= NULL
;
730 s
->ext
.tls13_cookie_len
= 0;
735 EXT_RETURN
tls_construct_ctos_early_data(SSL
*s
, WPACKET
*pkt
,
736 unsigned int context
, X509
*x
,
739 #ifndef OPENSSL_NO_PSK
740 char identity
[PSK_MAX_IDENTITY_LEN
+ 1];
741 #endif /* OPENSSL_NO_PSK */
742 const unsigned char *id
= NULL
;
744 SSL_SESSION
*psksess
= NULL
;
745 SSL_SESSION
*edsess
= NULL
;
746 const EVP_MD
*handmd
= NULL
;
748 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
749 handmd
= ssl_handshake_md(s
);
751 if (s
->psk_use_session_cb
!= NULL
752 && (!s
->psk_use_session_cb(s
, handmd
, &id
, &idlen
, &psksess
)
754 && psksess
->ssl_version
!= TLS1_3_VERSION
))) {
755 SSL_SESSION_free(psksess
);
756 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_PSK
);
757 return EXT_RETURN_FAIL
;
760 #ifndef OPENSSL_NO_PSK
761 if (psksess
== NULL
&& s
->psk_client_callback
!= NULL
) {
762 unsigned char psk
[PSK_MAX_PSK_LEN
];
765 memset(identity
, 0, sizeof(identity
));
766 psklen
= s
->psk_client_callback(s
, NULL
, identity
, sizeof(identity
) - 1,
769 if (psklen
> PSK_MAX_PSK_LEN
) {
770 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, ERR_R_INTERNAL_ERROR
);
771 return EXT_RETURN_FAIL
;
772 } else if (psklen
> 0) {
773 const unsigned char tls13_aes128gcmsha256_id
[] = { 0x13, 0x01 };
774 const SSL_CIPHER
*cipher
;
776 idlen
= strlen(identity
);
777 if (idlen
> PSK_MAX_IDENTITY_LEN
) {
778 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
779 return EXT_RETURN_FAIL
;
781 id
= (unsigned char *)identity
;
784 * We found a PSK using an old style callback. We don't know
785 * the digest so we default to SHA256 as per the TLSv1.3 spec
787 cipher
= SSL_CIPHER_find(s
, tls13_aes128gcmsha256_id
);
788 if (cipher
== NULL
) {
789 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
790 return EXT_RETURN_FAIL
;
793 psksess
= SSL_SESSION_new();
795 || !SSL_SESSION_set1_master_key(psksess
, psk
, psklen
)
796 || !SSL_SESSION_set_cipher(psksess
, cipher
)
797 || !SSL_SESSION_set_protocol_version(psksess
, TLS1_3_VERSION
)) {
798 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
799 OPENSSL_cleanse(psk
, psklen
);
800 return EXT_RETURN_FAIL
;
802 OPENSSL_cleanse(psk
, psklen
);
805 #endif /* OPENSSL_NO_PSK */
807 SSL_SESSION_free(s
->psksession
);
808 s
->psksession
= psksess
;
809 if (psksess
!= NULL
) {
810 OPENSSL_free(s
->psksession_id
);
811 s
->psksession_id
= OPENSSL_memdup(id
, idlen
);
812 if (s
->psksession_id
== NULL
) {
813 s
->psksession_id_len
= 0;
814 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
815 return EXT_RETURN_FAIL
;
817 s
->psksession_id_len
= idlen
;
820 if (s
->early_data_state
!= SSL_EARLY_DATA_CONNECTING
821 || (s
->session
->ext
.max_early_data
== 0
822 && (psksess
== NULL
|| psksess
->ext
.max_early_data
== 0))) {
823 s
->max_early_data
= 0;
824 return EXT_RETURN_NOT_SENT
;
826 edsess
= s
->session
->ext
.max_early_data
!= 0 ? s
->session
: psksess
;
827 s
->max_early_data
= edsess
->ext
.max_early_data
;
829 if (edsess
->ext
.hostname
!= NULL
) {
830 if (s
->ext
.hostname
== NULL
831 || (s
->ext
.hostname
!= NULL
832 && strcmp(s
->ext
.hostname
, edsess
->ext
.hostname
) != 0)) {
833 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
834 SSL_R_INCONSISTENT_EARLY_DATA_SNI
);
835 return EXT_RETURN_FAIL
;
839 if ((s
->ext
.alpn
== NULL
&& edsess
->ext
.alpn_selected
!= NULL
)) {
840 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_INCONSISTENT_EARLY_DATA_ALPN
);
841 return EXT_RETURN_FAIL
;
845 * Verify that we are offering an ALPN protocol consistent with the early
848 if (edsess
->ext
.alpn_selected
!= NULL
) {
849 PACKET prots
, alpnpkt
;
852 if (!PACKET_buf_init(&prots
, s
->ext
.alpn
, s
->ext
.alpn_len
)) {
853 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
854 return EXT_RETURN_FAIL
;
856 while (PACKET_get_length_prefixed_1(&prots
, &alpnpkt
)) {
857 if (PACKET_equal(&alpnpkt
, edsess
->ext
.alpn_selected
,
858 edsess
->ext
.alpn_selected_len
)) {
864 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
865 SSL_R_INCONSISTENT_EARLY_DATA_ALPN
);
866 return EXT_RETURN_FAIL
;
870 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_early_data
)
871 || !WPACKET_start_sub_packet_u16(pkt
)
872 || !WPACKET_close(pkt
)) {
873 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
874 return EXT_RETURN_FAIL
;
878 * We set this to rejected here. Later, if the server acknowledges the
879 * extension, we set it to accepted.
881 s
->ext
.early_data
= SSL_EARLY_DATA_REJECTED
;
882 s
->ext
.early_data_ok
= 1;
884 return EXT_RETURN_SENT
;
887 #define F5_WORKAROUND_MIN_MSG_LEN 0xff
888 #define F5_WORKAROUND_MAX_MSG_LEN 0x200
891 * PSK pre binder overhead =
892 * 2 bytes for TLSEXT_TYPE_psk
893 * 2 bytes for extension length
894 * 2 bytes for identities list length
895 * 2 bytes for identity length
896 * 4 bytes for obfuscated_ticket_age
897 * 2 bytes for binder list length
898 * 1 byte for binder length
899 * The above excludes the number of bytes for the identity itself and the
900 * subsequent binder bytes
902 #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
904 EXT_RETURN
tls_construct_ctos_padding(SSL
*s
, WPACKET
*pkt
,
905 unsigned int context
, X509
*x
,
908 unsigned char *padbytes
;
911 if ((s
->options
& SSL_OP_TLSEXT_PADDING
) == 0)
912 return EXT_RETURN_NOT_SENT
;
915 * Add padding to workaround bugs in F5 terminators. See RFC7685.
916 * This code calculates the length of all extensions added so far but
917 * excludes the PSK extension (because that MUST be written last). Therefore
918 * this extension MUST always appear second to last.
920 if (!WPACKET_get_total_written(pkt
, &hlen
)) {
921 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
922 return EXT_RETURN_FAIL
;
926 * If we're going to send a PSK then that will be written out after this
927 * extension, so we need to calculate how long it is going to be.
929 if (s
->session
->ssl_version
== TLS1_3_VERSION
930 && s
->session
->ext
.ticklen
!= 0
931 && s
->session
->cipher
!= NULL
) {
932 const EVP_MD
*md
= ssl_md(s
->ctx
, s
->session
->cipher
->algorithm2
);
936 * Add the fixed PSK overhead, the identity length and the binder
939 hlen
+= PSK_PRE_BINDER_OVERHEAD
+ s
->session
->ext
.ticklen
940 + EVP_MD_get_size(md
);
944 if (hlen
> F5_WORKAROUND_MIN_MSG_LEN
&& hlen
< F5_WORKAROUND_MAX_MSG_LEN
) {
945 /* Calculate the amount of padding we need to add */
946 hlen
= F5_WORKAROUND_MAX_MSG_LEN
- hlen
;
949 * Take off the size of extension header itself (2 bytes for type and
950 * 2 bytes for length bytes), but ensure that the extension is at least
951 * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
952 * 8.x are intolerant of that condition)
959 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_padding
)
960 || !WPACKET_sub_allocate_bytes_u16(pkt
, hlen
, &padbytes
)) {
961 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
962 return EXT_RETURN_FAIL
;
964 memset(padbytes
, 0, hlen
);
967 return EXT_RETURN_SENT
;
971 * Construct the pre_shared_key extension
973 EXT_RETURN
tls_construct_ctos_psk(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
974 X509
*x
, size_t chainidx
)
976 #ifndef OPENSSL_NO_TLS1_3
977 uint32_t now
, agesec
, agems
= 0;
978 size_t reshashsize
= 0, pskhashsize
= 0, binderoffset
, msglen
;
979 unsigned char *resbinder
= NULL
, *pskbinder
= NULL
, *msgstart
= NULL
;
980 const EVP_MD
*handmd
= NULL
, *mdres
= NULL
, *mdpsk
= NULL
;
983 s
->ext
.tick_identity
= 0;
986 * Note: At this stage of the code we only support adding a single
987 * resumption PSK. If we add support for multiple PSKs then the length
988 * calculations in the padding extension will need to be adjusted.
992 * If this is an incompatible or new session then we have nothing to resume
993 * so don't add this extension.
995 if (s
->session
->ssl_version
!= TLS1_3_VERSION
996 || (s
->session
->ext
.ticklen
== 0 && s
->psksession
== NULL
))
997 return EXT_RETURN_NOT_SENT
;
999 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
1000 handmd
= ssl_handshake_md(s
);
1002 if (s
->session
->ext
.ticklen
!= 0) {
1003 /* Get the digest associated with the ciphersuite in the session */
1004 if (s
->session
->cipher
== NULL
) {
1005 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1006 return EXT_RETURN_FAIL
;
1008 mdres
= ssl_md(s
->ctx
, s
->session
->cipher
->algorithm2
);
1009 if (mdres
== NULL
) {
1011 * Don't recognize this cipher so we can't use the session.
1017 if (s
->hello_retry_request
== SSL_HRR_PENDING
&& mdres
!= handmd
) {
1019 * Selected ciphersuite hash does not match the hash for the session
1020 * so we can't use it.
1026 * Technically the C standard just says time() returns a time_t and says
1027 * nothing about the encoding of that type. In practice most
1028 * implementations follow POSIX which holds it as an integral type in
1029 * seconds since epoch. We've already made the assumption that we can do
1030 * this in multiple places in the code, so portability shouldn't be an
1033 now
= (uint32_t)time(NULL
);
1034 agesec
= now
- (uint32_t)s
->session
->time
;
1036 * We calculate the age in seconds but the server may work in ms. Due to
1037 * rounding errors we could overestimate the age by up to 1s. It is
1038 * better to underestimate it. Otherwise, if the RTT is very short, when
1039 * the server calculates the age reported by the client it could be
1040 * bigger than the age calculated on the server - which should never
1046 if (s
->session
->ext
.tick_lifetime_hint
< agesec
) {
1047 /* Ticket is too old. Ignore it. */
1052 * Calculate age in ms. We're just doing it to nearest second. Should be
1055 agems
= agesec
* (uint32_t)1000;
1057 if (agesec
!= 0 && agems
/ (uint32_t)1000 != agesec
) {
1059 * Overflow. Shouldn't happen unless this is a *really* old session.
1060 * If so we just ignore it.
1066 * Obfuscate the age. Overflow here is fine, this addition is supposed
1069 agems
+= s
->session
->ext
.tick_age_add
;
1071 reshashsize
= EVP_MD_get_size(mdres
);
1072 s
->ext
.tick_identity
++;
1077 if (!dores
&& s
->psksession
== NULL
)
1078 return EXT_RETURN_NOT_SENT
;
1080 if (s
->psksession
!= NULL
) {
1081 mdpsk
= ssl_md(s
->ctx
, s
->psksession
->cipher
->algorithm2
);
1082 if (mdpsk
== NULL
) {
1084 * Don't recognize this cipher so we can't use the session.
1085 * If this happens it's an application bug.
1087 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_PSK
);
1088 return EXT_RETURN_FAIL
;
1091 if (s
->hello_retry_request
== SSL_HRR_PENDING
&& mdpsk
!= handmd
) {
1093 * Selected ciphersuite hash does not match the hash for the PSK
1094 * session. This is an application bug.
1096 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_PSK
);
1097 return EXT_RETURN_FAIL
;
1100 pskhashsize
= EVP_MD_get_size(mdpsk
);
1103 /* Create the extension, but skip over the binder for now */
1104 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_psk
)
1105 || !WPACKET_start_sub_packet_u16(pkt
)
1106 || !WPACKET_start_sub_packet_u16(pkt
)) {
1107 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1108 return EXT_RETURN_FAIL
;
1112 if (!WPACKET_sub_memcpy_u16(pkt
, s
->session
->ext
.tick
,
1113 s
->session
->ext
.ticklen
)
1114 || !WPACKET_put_bytes_u32(pkt
, agems
)) {
1115 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1116 return EXT_RETURN_FAIL
;
1120 if (s
->psksession
!= NULL
) {
1121 if (!WPACKET_sub_memcpy_u16(pkt
, s
->psksession_id
,
1122 s
->psksession_id_len
)
1123 || !WPACKET_put_bytes_u32(pkt
, 0)) {
1124 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1125 return EXT_RETURN_FAIL
;
1127 s
->ext
.tick_identity
++;
1130 if (!WPACKET_close(pkt
)
1131 || !WPACKET_get_total_written(pkt
, &binderoffset
)
1132 || !WPACKET_start_sub_packet_u16(pkt
)
1134 && !WPACKET_sub_allocate_bytes_u8(pkt
, reshashsize
, &resbinder
))
1135 || (s
->psksession
!= NULL
1136 && !WPACKET_sub_allocate_bytes_u8(pkt
, pskhashsize
, &pskbinder
))
1137 || !WPACKET_close(pkt
)
1138 || !WPACKET_close(pkt
)
1139 || !WPACKET_get_total_written(pkt
, &msglen
)
1141 * We need to fill in all the sub-packet lengths now so we can
1142 * calculate the HMAC of the message up to the binders
1144 || !WPACKET_fill_lengths(pkt
)) {
1145 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1146 return EXT_RETURN_FAIL
;
1149 msgstart
= WPACKET_get_curr(pkt
) - msglen
;
1152 && tls_psk_do_binder(s
, mdres
, msgstart
, binderoffset
, NULL
,
1153 resbinder
, s
->session
, 1, 0) != 1) {
1154 /* SSLfatal() already called */
1155 return EXT_RETURN_FAIL
;
1158 if (s
->psksession
!= NULL
1159 && tls_psk_do_binder(s
, mdpsk
, msgstart
, binderoffset
, NULL
,
1160 pskbinder
, s
->psksession
, 1, 1) != 1) {
1161 /* SSLfatal() already called */
1162 return EXT_RETURN_FAIL
;
1165 return EXT_RETURN_SENT
;
1167 return EXT_RETURN_NOT_SENT
;
1171 EXT_RETURN
tls_construct_ctos_post_handshake_auth(SSL
*s
, WPACKET
*pkt
,
1172 ossl_unused
unsigned int context
,
1173 ossl_unused X509
*x
,
1174 ossl_unused
size_t chainidx
)
1176 #ifndef OPENSSL_NO_TLS1_3
1177 if (!s
->pha_enabled
)
1178 return EXT_RETURN_NOT_SENT
;
1180 /* construct extension - 0 length, no contents */
1181 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_post_handshake_auth
)
1182 || !WPACKET_start_sub_packet_u16(pkt
)
1183 || !WPACKET_close(pkt
)) {
1184 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1185 return EXT_RETURN_FAIL
;
1188 s
->post_handshake_auth
= SSL_PHA_EXT_SENT
;
1190 return EXT_RETURN_SENT
;
1192 return EXT_RETURN_NOT_SENT
;
1198 * Parse the server's renegotiation binding and abort if it's not right
1200 int tls_parse_stoc_renegotiate(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1201 X509
*x
, size_t chainidx
)
1203 size_t expected_len
= s
->s3
.previous_client_finished_len
1204 + s
->s3
.previous_server_finished_len
;
1206 const unsigned char *data
;
1208 /* Check for logic errors */
1209 if (!ossl_assert(expected_len
== 0
1210 || s
->s3
.previous_client_finished_len
!= 0)
1211 || !ossl_assert(expected_len
== 0
1212 || s
->s3
.previous_server_finished_len
!= 0)) {
1213 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1217 /* Parse the length byte */
1218 if (!PACKET_get_1_len(pkt
, &ilen
)) {
1219 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RENEGOTIATION_ENCODING_ERR
);
1223 /* Consistency check */
1224 if (PACKET_remaining(pkt
) != ilen
) {
1225 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_RENEGOTIATION_ENCODING_ERR
);
1229 /* Check that the extension matches */
1230 if (ilen
!= expected_len
) {
1231 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_RENEGOTIATION_MISMATCH
);
1235 if (!PACKET_get_bytes(pkt
, &data
, s
->s3
.previous_client_finished_len
)
1236 || memcmp(data
, s
->s3
.previous_client_finished
,
1237 s
->s3
.previous_client_finished_len
) != 0) {
1238 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_RENEGOTIATION_MISMATCH
);
1242 if (!PACKET_get_bytes(pkt
, &data
, s
->s3
.previous_server_finished_len
)
1243 || memcmp(data
, s
->s3
.previous_server_finished
,
1244 s
->s3
.previous_server_finished_len
) != 0) {
1245 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_RENEGOTIATION_MISMATCH
);
1248 s
->s3
.send_connection_binding
= 1;
1253 /* Parse the server's max fragment len extension packet */
1254 int tls_parse_stoc_maxfragmentlen(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1255 X509
*x
, size_t chainidx
)
1259 if (PACKET_remaining(pkt
) != 1 || !PACKET_get_1(pkt
, &value
)) {
1260 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1264 /* |value| should contains a valid max-fragment-length code. */
1265 if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value
)) {
1266 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1267 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH
);
1271 /* Must be the same value as client-configured one who was sent to server */
1273 * RFC 6066: if a client receives a maximum fragment length negotiation
1274 * response that differs from the length it requested, ...
1275 * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1277 if (value
!= s
->ext
.max_fragment_len_mode
) {
1278 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1279 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH
);
1284 * Maximum Fragment Length Negotiation succeeded.
1285 * The negotiated Maximum Fragment Length is binding now.
1287 s
->session
->ext
.max_fragment_len_mode
= value
;
1292 int tls_parse_stoc_server_name(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1293 X509
*x
, size_t chainidx
)
1295 if (s
->ext
.hostname
== NULL
) {
1296 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1300 if (PACKET_remaining(pkt
) > 0) {
1301 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1306 if (s
->session
->ext
.hostname
!= NULL
) {
1307 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1310 s
->session
->ext
.hostname
= OPENSSL_strdup(s
->ext
.hostname
);
1311 if (s
->session
->ext
.hostname
== NULL
) {
1312 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1320 int tls_parse_stoc_ec_pt_formats(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1321 X509
*x
, size_t chainidx
)
1323 size_t ecpointformats_len
;
1324 PACKET ecptformatlist
;
1326 if (!PACKET_as_length_prefixed_1(pkt
, &ecptformatlist
)) {
1327 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1331 ecpointformats_len
= PACKET_remaining(&ecptformatlist
);
1332 if (ecpointformats_len
== 0) {
1333 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
1337 s
->ext
.peer_ecpointformats_len
= 0;
1338 OPENSSL_free(s
->ext
.peer_ecpointformats
);
1339 s
->ext
.peer_ecpointformats
= OPENSSL_malloc(ecpointformats_len
);
1340 if (s
->ext
.peer_ecpointformats
== NULL
) {
1341 s
->ext
.peer_ecpointformats_len
= 0;
1342 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1346 s
->ext
.peer_ecpointformats_len
= ecpointformats_len
;
1348 if (!PACKET_copy_bytes(&ecptformatlist
,
1349 s
->ext
.peer_ecpointformats
,
1350 ecpointformats_len
)) {
1351 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1359 int tls_parse_stoc_session_ticket(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1360 X509
*x
, size_t chainidx
)
1362 if (s
->ext
.session_ticket_cb
!= NULL
&&
1363 !s
->ext
.session_ticket_cb(s
, PACKET_data(pkt
),
1364 PACKET_remaining(pkt
),
1365 s
->ext
.session_ticket_cb_arg
)) {
1366 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_BAD_EXTENSION
);
1370 if (!tls_use_ticket(s
)) {
1371 SSLfatal(s
, SSL_AD_UNSUPPORTED_EXTENSION
, SSL_R_BAD_EXTENSION
);
1374 if (PACKET_remaining(pkt
) > 0) {
1375 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1379 s
->ext
.ticket_expected
= 1;
1384 #ifndef OPENSSL_NO_OCSP
1385 int tls_parse_stoc_status_request(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1386 X509
*x
, size_t chainidx
)
1388 if (context
== SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
) {
1389 /* We ignore this if the server sends a CertificateRequest */
1390 /* TODO(TLS1.3): Add support for this */
1395 * MUST only be sent if we've requested a status
1396 * request message. In TLS <= 1.2 it must also be empty.
1398 if (s
->ext
.status_type
!= TLSEXT_STATUSTYPE_ocsp
) {
1399 SSLfatal(s
, SSL_AD_UNSUPPORTED_EXTENSION
, SSL_R_BAD_EXTENSION
);
1402 if (!SSL_IS_TLS13(s
) && PACKET_remaining(pkt
) > 0) {
1403 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1407 if (SSL_IS_TLS13(s
)) {
1408 /* We only know how to handle this if it's for the first Certificate in
1409 * the chain. We ignore any other responses.
1414 /* SSLfatal() already called */
1415 return tls_process_cert_status_body(s
, pkt
);
1418 /* Set flag to expect CertificateStatus message */
1419 s
->ext
.status_expected
= 1;
1426 #ifndef OPENSSL_NO_CT
1427 int tls_parse_stoc_sct(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1430 if (context
== SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
) {
1431 /* We ignore this if the server sends it in a CertificateRequest */
1432 /* TODO(TLS1.3): Add support for this */
1437 * Only take it if we asked for it - i.e if there is no CT validation
1438 * callback set, then a custom extension MAY be processing it, so we
1439 * need to let control continue to flow to that.
1441 if (s
->ct_validation_callback
!= NULL
) {
1442 size_t size
= PACKET_remaining(pkt
);
1444 /* Simply copy it off for later processing */
1445 OPENSSL_free(s
->ext
.scts
);
1448 s
->ext
.scts_len
= (uint16_t)size
;
1450 s
->ext
.scts
= OPENSSL_malloc(size
);
1451 if (s
->ext
.scts
== NULL
) {
1452 s
->ext
.scts_len
= 0;
1453 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
1456 if (!PACKET_copy_bytes(pkt
, s
->ext
.scts
, size
)) {
1457 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1462 ENDPOINT role
= (context
& SSL_EXT_TLS1_2_SERVER_HELLO
) != 0
1463 ? ENDPOINT_CLIENT
: ENDPOINT_BOTH
;
1466 * If we didn't ask for it then there must be a custom extension,
1467 * otherwise this is unsolicited.
1469 if (custom_ext_find(&s
->cert
->custext
, role
,
1470 TLSEXT_TYPE_signed_certificate_timestamp
,
1472 SSLfatal(s
, TLS1_AD_UNSUPPORTED_EXTENSION
, SSL_R_BAD_EXTENSION
);
1476 if (!custom_ext_parse(s
, context
,
1477 TLSEXT_TYPE_signed_certificate_timestamp
,
1478 PACKET_data(pkt
), PACKET_remaining(pkt
),
1480 /* SSLfatal already called */
1490 #ifndef OPENSSL_NO_NEXTPROTONEG
1492 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1493 * elements of zero length are allowed and the set of elements must exactly
1494 * fill the length of the block. Returns 1 on success or 0 on failure.
1496 static int ssl_next_proto_validate(SSL
*s
, PACKET
*pkt
)
1498 PACKET tmp_protocol
;
1500 while (PACKET_remaining(pkt
)) {
1501 if (!PACKET_get_length_prefixed_1(pkt
, &tmp_protocol
)
1502 || PACKET_remaining(&tmp_protocol
) == 0) {
1503 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1511 int tls_parse_stoc_npn(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1514 unsigned char *selected
;
1515 unsigned char selected_len
;
1518 /* Check if we are in a renegotiation. If so ignore this extension */
1519 if (!SSL_IS_FIRST_HANDSHAKE(s
))
1522 /* We must have requested it. */
1523 if (s
->ctx
->ext
.npn_select_cb
== NULL
) {
1524 SSLfatal(s
, SSL_AD_UNSUPPORTED_EXTENSION
, SSL_R_BAD_EXTENSION
);
1528 /* The data must be valid */
1530 if (!ssl_next_proto_validate(s
, &tmppkt
)) {
1531 /* SSLfatal() already called */
1534 if (s
->ctx
->ext
.npn_select_cb(s
, &selected
, &selected_len
,
1536 PACKET_remaining(pkt
),
1537 s
->ctx
->ext
.npn_select_cb_arg
) !=
1538 SSL_TLSEXT_ERR_OK
) {
1539 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_BAD_EXTENSION
);
1544 * Could be non-NULL if server has sent multiple NPN extensions in
1545 * a single Serverhello
1547 OPENSSL_free(s
->ext
.npn
);
1548 s
->ext
.npn
= OPENSSL_malloc(selected_len
);
1549 if (s
->ext
.npn
== NULL
) {
1551 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1555 memcpy(s
->ext
.npn
, selected
, selected_len
);
1556 s
->ext
.npn_len
= selected_len
;
1563 int tls_parse_stoc_alpn(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1568 /* We must have requested it. */
1569 if (!s
->s3
.alpn_sent
) {
1570 SSLfatal(s
, SSL_AD_UNSUPPORTED_EXTENSION
, SSL_R_BAD_EXTENSION
);
1574 * The extension data consists of:
1575 * uint16 list_length
1576 * uint8 proto_length;
1577 * uint8 proto[proto_length];
1579 if (!PACKET_get_net_2_len(pkt
, &len
)
1580 || PACKET_remaining(pkt
) != len
|| !PACKET_get_1_len(pkt
, &len
)
1581 || PACKET_remaining(pkt
) != len
) {
1582 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1585 OPENSSL_free(s
->s3
.alpn_selected
);
1586 s
->s3
.alpn_selected
= OPENSSL_malloc(len
);
1587 if (s
->s3
.alpn_selected
== NULL
) {
1588 s
->s3
.alpn_selected_len
= 0;
1589 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1592 if (!PACKET_copy_bytes(pkt
, s
->s3
.alpn_selected
, len
)) {
1593 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1596 s
->s3
.alpn_selected_len
= len
;
1598 if (s
->session
->ext
.alpn_selected
== NULL
1599 || s
->session
->ext
.alpn_selected_len
!= len
1600 || memcmp(s
->session
->ext
.alpn_selected
, s
->s3
.alpn_selected
, len
)
1602 /* ALPN not consistent with the old session so cannot use early_data */
1603 s
->ext
.early_data_ok
= 0;
1607 * This is a new session and so alpn_selected should have been
1608 * initialised to NULL. We should update it with the selected ALPN.
1610 if (!ossl_assert(s
->session
->ext
.alpn_selected
== NULL
)) {
1611 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1614 s
->session
->ext
.alpn_selected
=
1615 OPENSSL_memdup(s
->s3
.alpn_selected
, s
->s3
.alpn_selected_len
);
1616 if (s
->session
->ext
.alpn_selected
== NULL
) {
1617 s
->session
->ext
.alpn_selected_len
= 0;
1618 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1621 s
->session
->ext
.alpn_selected_len
= s
->s3
.alpn_selected_len
;
1627 #ifndef OPENSSL_NO_SRTP
1628 int tls_parse_stoc_use_srtp(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1631 unsigned int id
, ct
, mki
;
1633 STACK_OF(SRTP_PROTECTION_PROFILE
) *clnt
;
1634 SRTP_PROTECTION_PROFILE
*prof
;
1636 if (!PACKET_get_net_2(pkt
, &ct
) || ct
!= 2
1637 || !PACKET_get_net_2(pkt
, &id
)
1638 || !PACKET_get_1(pkt
, &mki
)
1639 || PACKET_remaining(pkt
) != 0) {
1640 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1641 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST
);
1646 /* Must be no MKI, since we never offer one */
1647 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_SRTP_MKI_VALUE
);
1651 /* Throw an error if the server gave us an unsolicited extension */
1652 clnt
= SSL_get_srtp_profiles(s
);
1654 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_NO_SRTP_PROFILES
);
1659 * Check to see if the server gave us something we support (and
1660 * presumably offered)
1662 for (i
= 0; i
< sk_SRTP_PROTECTION_PROFILE_num(clnt
); i
++) {
1663 prof
= sk_SRTP_PROTECTION_PROFILE_value(clnt
, i
);
1665 if (prof
->id
== id
) {
1666 s
->srtp_profile
= prof
;
1671 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
1672 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST
);
1677 int tls_parse_stoc_etm(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1680 /* Ignore if inappropriate ciphersuite */
1681 if (!(s
->options
& SSL_OP_NO_ENCRYPT_THEN_MAC
)
1682 && s
->s3
.tmp
.new_cipher
->algorithm_mac
!= SSL_AEAD
1683 && s
->s3
.tmp
.new_cipher
->algorithm_enc
!= SSL_RC4
)
1689 int tls_parse_stoc_ems(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1692 if (s
->options
& SSL_OP_NO_EXTENDED_MASTER_SECRET
)
1694 s
->s3
.flags
|= TLS1_FLAGS_RECEIVED_EXTMS
;
1696 s
->session
->flags
|= SSL_SESS_FLAG_EXTMS
;
1701 int tls_parse_stoc_supported_versions(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1702 X509
*x
, size_t chainidx
)
1704 unsigned int version
;
1706 if (!PACKET_get_net_2(pkt
, &version
)
1707 || PACKET_remaining(pkt
) != 0) {
1708 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1713 * The only protocol version we support which is valid in this extension in
1714 * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
1716 if (version
!= TLS1_3_VERSION
) {
1717 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1718 SSL_R_BAD_PROTOCOL_VERSION_NUMBER
);
1722 /* We ignore this extension for HRRs except to sanity check it */
1723 if (context
== SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
)
1726 /* We just set it here. We validate it in ssl_choose_client_version */
1727 s
->version
= version
;
1732 int tls_parse_stoc_key_share(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1735 #ifndef OPENSSL_NO_TLS1_3
1736 unsigned int group_id
;
1738 EVP_PKEY
*ckey
= s
->s3
.tmp
.pkey
, *skey
= NULL
;
1739 const TLS_GROUP_INFO
*ginf
= NULL
;
1742 if (ckey
== NULL
|| s
->s3
.peer_tmp
!= NULL
) {
1743 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1747 if (!PACKET_get_net_2(pkt
, &group_id
)) {
1748 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1752 if ((context
& SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
) != 0) {
1753 const uint16_t *pgroups
= NULL
;
1754 size_t i
, num_groups
;
1756 if (PACKET_remaining(pkt
) != 0) {
1757 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1762 * It is an error if the HelloRetryRequest wants a key_share that we
1763 * already sent in the first ClientHello
1765 if (group_id
== s
->s3
.group_id
) {
1766 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_KEY_SHARE
);
1770 /* Validate the selected group is one we support */
1771 tls1_get_supported_groups(s
, &pgroups
, &num_groups
);
1772 for (i
= 0; i
< num_groups
; i
++) {
1773 if (group_id
== pgroups
[i
])
1777 || !tls_group_allowed(s
, group_id
, SSL_SECOP_CURVE_SUPPORTED
)) {
1778 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_KEY_SHARE
);
1782 s
->s3
.group_id
= group_id
;
1783 EVP_PKEY_free(s
->s3
.tmp
.pkey
);
1784 s
->s3
.tmp
.pkey
= NULL
;
1788 if (group_id
!= s
->s3
.group_id
) {
1790 * This isn't for the group that we sent in the original
1793 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_KEY_SHARE
);
1796 /* Retain this group in the SSL_SESSION */
1798 s
->session
->kex_group
= group_id
;
1799 } else if (group_id
!= s
->session
->kex_group
) {
1801 * If this is a resumption but changed what group was used, we need
1802 * to record the new group in the session, but the session is not
1803 * a new session and could be in use by other threads. So, make
1804 * a copy of the session to record the new information so that it's
1805 * useful for any sessions resumed from tickets issued on this
1808 SSL_SESSION
*new_sess
;
1810 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == NULL
) {
1811 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
1814 SSL_SESSION_free(s
->session
);
1815 s
->session
= new_sess
;
1816 s
->session
->kex_group
= group_id
;
1819 if ((ginf
= tls1_group_id_lookup(s
->ctx
, group_id
)) == NULL
) {
1820 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_KEY_SHARE
);
1824 if (!PACKET_as_length_prefixed_2(pkt
, &encoded_pt
)
1825 || PACKET_remaining(&encoded_pt
) == 0) {
1826 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1830 if (!ginf
->is_kem
) {
1832 skey
= EVP_PKEY_new();
1833 if (skey
== NULL
|| EVP_PKEY_copy_parameters(skey
, ckey
) <= 0) {
1834 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_COPY_PARAMETERS_FAILED
);
1838 if (EVP_PKEY_set1_encoded_public_key(skey
, PACKET_data(&encoded_pt
),
1839 PACKET_remaining(&encoded_pt
)) <= 0) {
1840 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_ECPOINT
);
1841 EVP_PKEY_free(skey
);
1845 if (ssl_derive(s
, ckey
, skey
, 1) == 0) {
1846 /* SSLfatal() already called */
1847 EVP_PKEY_free(skey
);
1850 s
->s3
.peer_tmp
= skey
;
1853 const unsigned char *ct
= PACKET_data(&encoded_pt
);
1854 size_t ctlen
= PACKET_remaining(&encoded_pt
);
1856 if (ssl_decapsulate(s
, ckey
, ct
, ctlen
, 1) == 0) {
1857 /* SSLfatal() already called */
1867 int tls_parse_stoc_cookie(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1872 if (!PACKET_as_length_prefixed_2(pkt
, &cookie
)
1873 || !PACKET_memdup(&cookie
, &s
->ext
.tls13_cookie
,
1874 &s
->ext
.tls13_cookie_len
)) {
1875 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1882 int tls_parse_stoc_early_data(SSL
*s
, PACKET
*pkt
, unsigned int context
,
1883 X509
*x
, size_t chainidx
)
1885 if (context
== SSL_EXT_TLS1_3_NEW_SESSION_TICKET
) {
1886 unsigned long max_early_data
;
1888 if (!PACKET_get_net_4(pkt
, &max_early_data
)
1889 || PACKET_remaining(pkt
) != 0) {
1890 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_MAX_EARLY_DATA
);
1894 s
->session
->ext
.max_early_data
= max_early_data
;
1899 if (PACKET_remaining(pkt
) != 0) {
1900 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1904 if (!s
->ext
.early_data_ok
1907 * If we get here then we didn't send early data, or we didn't resume
1908 * using the first identity, or the SNI/ALPN is not consistent so the
1909 * server should not be accepting it.
1911 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_EXTENSION
);
1915 s
->ext
.early_data
= SSL_EARLY_DATA_ACCEPTED
;
1920 int tls_parse_stoc_psk(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
1923 #ifndef OPENSSL_NO_TLS1_3
1924 unsigned int identity
;
1926 if (!PACKET_get_net_2(pkt
, &identity
) || PACKET_remaining(pkt
) != 0) {
1927 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1931 if (identity
>= (unsigned int)s
->ext
.tick_identity
) {
1932 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_PSK_IDENTITY
);
1937 * Session resumption tickets are always sent before PSK tickets. If the
1938 * ticket index is 0 then it must be for a session resumption ticket if we
1939 * sent two tickets, or if we didn't send a PSK ticket.
1941 if (identity
== 0 && (s
->psksession
== NULL
|| s
->ext
.tick_identity
== 2)) {
1943 SSL_SESSION_free(s
->psksession
);
1944 s
->psksession
= NULL
;
1948 if (s
->psksession
== NULL
) {
1949 /* Should never happen */
1950 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1955 * If we used the external PSK for sending early_data then s->early_secret
1956 * is already set up, so don't overwrite it. Otherwise we copy the
1957 * early_secret across that we generated earlier.
1959 if ((s
->early_data_state
!= SSL_EARLY_DATA_WRITE_RETRY
1960 && s
->early_data_state
!= SSL_EARLY_DATA_FINISHED_WRITING
)
1961 || s
->session
->ext
.max_early_data
> 0
1962 || s
->psksession
->ext
.max_early_data
== 0)
1963 memcpy(s
->early_secret
, s
->psksession
->early_secret
, EVP_MAX_MD_SIZE
);
1965 SSL_SESSION_free(s
->session
);
1966 s
->session
= s
->psksession
;
1967 s
->psksession
= NULL
;
1969 /* Early data is only allowed if we used the first ticket */
1971 s
->ext
.early_data_ok
= 0;