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