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