Path: blob/main/crypto/openssl/ssl/statem/extensions_srvr.c
105152 views
/*1* Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <openssl/ocsp.h>10#include "../ssl_local.h"11#include "statem_local.h"12#include "internal/cryptlib.h"13#include "internal/ssl_unwrap.h"1415#define COOKIE_STATE_FORMAT_VERSION 11617/*18* 2 bytes for packet length, 2 bytes for format version, 2 bytes for19* protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for20* key_share present flag, 8 bytes for timestamp, 2 bytes for the hashlen,21* EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie22* length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.23*/24#define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 8 + 2 + EVP_MAX_MD_SIZE + 1 \25+ SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)2627/*28* Message header + 2 bytes for protocol version + number of random bytes +29* + 1 byte for legacy session id length + number of bytes in legacy session id30* + 2 bytes for ciphersuite + 1 byte for legacy compression31* + 2 bytes for extension block length + 6 bytes for key_share extension32* + 4 bytes for cookie extension header + the number of bytes in the cookie33*/34#define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \35+ SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \36+ MAX_COOKIE_SIZE)3738/*39* Parse the client's renegotiation binding and abort if it's not right40*/41int tls_parse_ctos_renegotiate(SSL_CONNECTION *s, PACKET *pkt,42unsigned int context,43X509 *x, size_t chainidx)44{45unsigned int ilen;46const unsigned char *data;47int ok;4849/* Parse the length byte */50if (!PACKET_get_1(pkt, &ilen)51|| !PACKET_get_bytes(pkt, &data, ilen)) {52SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);53return 0;54}5556/* Check that the extension matches */57if (ilen != s->s3.previous_client_finished_len) {58SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);59return 0;60}6162ok = memcmp(data, s->s3.previous_client_finished,63s->s3.previous_client_finished_len);64#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION65if (ok) {66if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) {67ok = 0;68}69}70#endif71if (ok) {72SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);73return 0;74}7576s->s3.send_connection_binding = 1;7778return 1;79}8081/*-82* The servername extension is treated as follows:83*84* - Only the hostname type is supported with a maximum length of 255.85* - The servername is rejected if too long or if it contains zeros,86* in which case an fatal alert is generated.87* - The servername field is maintained together with the session cache.88* - When a session is resumed, the servername call back invoked in order89* to allow the application to position itself to the right context.90* - The servername is acknowledged if it is new for a session or when91* it is identical to a previously used for the same session.92* Applications can control the behaviour. They can at any time93* set a 'desirable' servername for a new SSL object. This can be the94* case for example with HTTPS when a Host: header field is received and95* a renegotiation is requested. In this case, a possible servername96* presented in the new client hello is only acknowledged if it matches97* the value of the Host: field.98* - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION99* if they provide for changing an explicit servername context for the100* session, i.e. when the session has been established with a servername101* extension.102* - On session reconnect, the servername extension may be absent.103*/104int tls_parse_ctos_server_name(SSL_CONNECTION *s, PACKET *pkt,105unsigned int context, X509 *x, size_t chainidx)106{107unsigned int servname_type;108PACKET sni, hostname;109110if (!PACKET_as_length_prefixed_2(pkt, &sni)111/* ServerNameList must be at least 1 byte long. */112|| PACKET_remaining(&sni) == 0) {113SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);114return 0;115}116117/*118* Although the intent was for server_name to be extensible, RFC 4366119* was not clear about it; and so OpenSSL among other implementations,120* always and only allows a 'host_name' name types.121* RFC 6066 corrected the mistake but adding new name types122* is nevertheless no longer feasible, so act as if no other123* SNI types can exist, to simplify parsing.124*125* Also note that the RFC permits only one SNI value per type,126* i.e., we can only have a single hostname.127*/128if (!PACKET_get_1(&sni, &servname_type)129|| servname_type != TLSEXT_NAMETYPE_host_name130|| !PACKET_as_length_prefixed_2(&sni, &hostname)) {131SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);132return 0;133}134135/*136* In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3137* we always use the SNI value from the handshake.138*/139if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {140if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {141SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);142return 0;143}144145if (PACKET_contains_zero_byte(&hostname)) {146SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);147return 0;148}149150/*151* Store the requested SNI in the SSL as temporary storage.152* If we accept it, it will get stored in the SSL_SESSION as well.153*/154OPENSSL_free(s->ext.hostname);155s->ext.hostname = NULL;156if (!PACKET_strndup(&hostname, &s->ext.hostname)) {157SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);158return 0;159}160161s->servername_done = 1;162} else {163/*164* In TLSv1.2 and below we should check if the SNI is consistent between165* the initial handshake and the resumption. In TLSv1.3 SNI is not166* associated with the session.167*/168s->servername_done = (s->session->ext.hostname != NULL)169&& PACKET_equal(&hostname, s->session->ext.hostname,170strlen(s->session->ext.hostname));171}172173return 1;174}175176int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,177unsigned int context,178X509 *x, size_t chainidx)179{180unsigned int value;181182if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {183SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);184return 0;185}186187/* Received |value| should be a valid max-fragment-length code. */188if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {189SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,190SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);191return 0;192}193194/*195* When doing a full handshake or a renegotiation max_fragment_len_mode will196* be TLSEXT_max_fragment_length_UNSPECIFIED197*198* In case of a resumption max_fragment_len_mode will be one of199* TLSEXT_max_fragment_length_DISABLED, TLSEXT_max_fragment_length_512,200* TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048.201* TLSEXT_max_fragment_length_4096202*203* RFC 6066: The negotiated length applies for the duration of the session204* including session resumptions.205*206* So we only set the value in case it is unspecified.207*/208if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)209/*210* Store it in session, so it'll become binding for us211* and we'll include it in a next Server Hello.212*/213s->session->ext.max_fragment_len_mode = value;214215return 1;216}217218#ifndef OPENSSL_NO_SRP219int tls_parse_ctos_srp(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,220X509 *x, size_t chainidx)221{222PACKET srp_I;223224if (!PACKET_as_length_prefixed_1(pkt, &srp_I)225|| PACKET_contains_zero_byte(&srp_I)) {226SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);227return 0;228}229230if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {231SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);232return 0;233}234235return 1;236}237#endif238239int tls_parse_ctos_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,240unsigned int context,241X509 *x, size_t chainidx)242{243PACKET ec_point_format_list;244245if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)246|| PACKET_remaining(&ec_point_format_list) == 0) {247SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);248return 0;249}250251if (!s->hit) {252if (!PACKET_memdup(&ec_point_format_list,253&s->ext.peer_ecpointformats,254&s->ext.peer_ecpointformats_len)) {255SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);256return 0;257}258}259260return 1;261}262263int tls_parse_ctos_session_ticket(SSL_CONNECTION *s, PACKET *pkt,264unsigned int context,265X509 *x, size_t chainidx)266{267if (s->ext.session_ticket_cb && !s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), PACKET_data(pkt), PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) {268SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);269return 0;270}271272return 1;273}274275int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt,276ossl_unused unsigned int context,277ossl_unused X509 *x,278ossl_unused size_t chainidx)279{280PACKET supported_sig_algs;281282if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)283|| PACKET_remaining(&supported_sig_algs) == 0) {284SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);285return 0;286}287288/*289* We use this routine on both clients and servers, and when clients290* get asked for PHA we need to always save the sigalgs regardless291* of whether it was a resumption or not.292*/293if ((!s->server || (s->server && !s->hit))294&& !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {295SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);296return 0;297}298299return 1;300}301302int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,303unsigned int context, X509 *x, size_t chainidx)304{305PACKET supported_sig_algs;306307if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)308|| PACKET_remaining(&supported_sig_algs) == 0) {309SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);310return 0;311}312313/*314* We use this routine on both clients and servers, and when clients315* get asked for PHA we need to always save the sigalgs regardless316* of whether it was a resumption or not.317*/318if ((!s->server || (s->server && !s->hit))319&& !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {320SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);321return 0;322}323324return 1;325}326327#ifndef OPENSSL_NO_OCSP328int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt,329unsigned int context,330X509 *x, size_t chainidx)331{332PACKET responder_id_list, exts;333334/* We ignore this in a resumption handshake */335if (s->hit)336return 1;337338/* Not defined if we get one of these in a client Certificate */339if (x != NULL)340return 1;341342if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {343SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);344return 0;345}346347if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {348/*349* We don't know what to do with any other type so ignore it.350*/351s->ext.status_type = TLSEXT_STATUSTYPE_nothing;352return 1;353}354355if (!PACKET_get_length_prefixed_2(pkt, &responder_id_list)) {356SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);357return 0;358}359360/*361* We remove any OCSP_RESPIDs from a previous handshake362* to prevent unbounded memory growth - CVE-2016-6304363*/364sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);365if (PACKET_remaining(&responder_id_list) > 0) {366s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();367if (s->ext.ocsp.ids == NULL) {368SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);369return 0;370}371} else {372s->ext.ocsp.ids = NULL;373}374375while (PACKET_remaining(&responder_id_list) > 0) {376OCSP_RESPID *id;377PACKET responder_id;378const unsigned char *id_data;379380if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)381|| PACKET_remaining(&responder_id) == 0) {382SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);383return 0;384}385386id_data = PACKET_data(&responder_id);387id = d2i_OCSP_RESPID(NULL, &id_data,388(int)PACKET_remaining(&responder_id));389if (id == NULL) {390SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);391return 0;392}393394if (id_data != PACKET_end(&responder_id)) {395OCSP_RESPID_free(id);396SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);397398return 0;399}400401if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {402OCSP_RESPID_free(id);403SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);404405return 0;406}407}408409/* Read in request_extensions */410if (!PACKET_as_length_prefixed_2(pkt, &exts)) {411SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);412return 0;413}414415if (PACKET_remaining(&exts) > 0) {416const unsigned char *ext_data = PACKET_data(&exts);417418sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,419X509_EXTENSION_free);420s->ext.ocsp.exts = d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));421if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {422SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);423return 0;424}425}426427return 1;428}429#endif430431#ifndef OPENSSL_NO_NEXTPROTONEG432int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,433X509 *x, size_t chainidx)434{435/*436* We shouldn't accept this extension on a437* renegotiation.438*/439if (SSL_IS_FIRST_HANDSHAKE(s))440s->s3.npn_seen = 1;441442return 1;443}444#endif445446/*447* Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN448* extension, not including type and length. Returns: 1 on success, 0 on error.449*/450int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,451X509 *x, size_t chainidx)452{453PACKET protocol_list, save_protocol_list, protocol;454455if (!SSL_IS_FIRST_HANDSHAKE(s))456return 1;457458if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)459|| PACKET_remaining(&protocol_list) < 2) {460SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);461return 0;462}463464save_protocol_list = protocol_list;465do {466/* Protocol names can't be empty. */467if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)468|| PACKET_remaining(&protocol) == 0) {469SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);470return 0;471}472} while (PACKET_remaining(&protocol_list) != 0);473474OPENSSL_free(s->s3.alpn_proposed);475s->s3.alpn_proposed = NULL;476s->s3.alpn_proposed_len = 0;477if (!PACKET_memdup(&save_protocol_list,478&s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) {479SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);480return 0;481}482483return 1;484}485486#ifndef OPENSSL_NO_SRTP487int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt,488unsigned int context, X509 *x, size_t chainidx)489{490STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;491unsigned int ct, mki_len, id;492int i, srtp_pref;493PACKET subpkt;494SSL *ssl = SSL_CONNECTION_GET_SSL(s);495496/* Ignore this if we have no SRTP profiles */497if (SSL_get_srtp_profiles(ssl) == NULL)498return 1;499500/* Pull off the length of the cipher suite list and check it is even */501if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0502|| !PACKET_get_sub_packet(pkt, &subpkt, ct)) {503SSLfatal(s, SSL_AD_DECODE_ERROR,504SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);505return 0;506}507508srvr = SSL_get_srtp_profiles(ssl);509s->srtp_profile = NULL;510/* Search all profiles for a match initially */511srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);512513while (PACKET_remaining(&subpkt)) {514if (!PACKET_get_net_2(&subpkt, &id)) {515SSLfatal(s, SSL_AD_DECODE_ERROR,516SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);517return 0;518}519520/*521* Only look for match in profiles of higher preference than522* current match.523* If no profiles have been have been configured then this524* does nothing.525*/526for (i = 0; i < srtp_pref; i++) {527SRTP_PROTECTION_PROFILE *sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i);528529if (sprof->id == id) {530s->srtp_profile = sprof;531srtp_pref = i;532break;533}534}535}536537/* Now extract the MKI value as a sanity check, but discard it for now */538if (!PACKET_get_1(pkt, &mki_len)) {539SSLfatal(s, SSL_AD_DECODE_ERROR,540SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);541return 0;542}543544if (!PACKET_forward(pkt, mki_len)545|| PACKET_remaining(pkt)) {546SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);547return 0;548}549550return 1;551}552#endif553554int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,555X509 *x, size_t chainidx)556{557if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))558s->ext.use_etm = 1;559560return 1;561}562563/*564* Process a psk_kex_modes extension received in the ClientHello. |pkt| contains565* the raw PACKET data for the extension. Returns 1 on success or 0 on failure.566*/567int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt,568unsigned int context,569X509 *x, size_t chainidx)570{571#ifndef OPENSSL_NO_TLS1_3572PACKET psk_kex_modes;573unsigned int mode;574575if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)576|| PACKET_remaining(&psk_kex_modes) == 0) {577SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);578return 0;579}580581while (PACKET_get_1(&psk_kex_modes, &mode)) {582if (mode == TLSEXT_KEX_MODE_KE_DHE)583s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;584else if (mode == TLSEXT_KEX_MODE_KE585&& (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)586s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;587}588589if (((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) != 0)590&& (s->options & SSL_OP_PREFER_NO_DHE_KEX) != 0) {591592/*593* If NO_DHE is supported and preferred, then we only remember this594* mode. DHE PSK will not be used for sure, because in any case where595* it would be supported (i.e. if a key share is present), NO_DHE would596* be supported as well. As the latter is preferred it would be597* chosen. By removing DHE PSK here, we don't have to deal with the598* SSL_OP_PREFER_NO_DHE_KEX option in any other place.599*/600s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE;601}602603#endif604605return 1;606}607608/*609* Use function tls_parse_ctos_key_share with helper functions extract_keyshares,610* check_overlap and tls_accept_ksgroup to parse the key_share extension(s)611* received in the ClientHello and to select the group used of the key exchange612*/613614#ifndef OPENSSL_NO_TLS1_3615/*616* Accept a key share group by setting the related variables in s->s3 and617* by generating a pubkey for this group618*/619static int tls_accept_ksgroup(SSL_CONNECTION *s, uint16_t ksgroup, PACKET *encoded_pubkey)620{621/* Accept the key share group */622s->s3.group_id = ksgroup;623s->s3.group_id_candidate = ksgroup;624/* Cache the selected group ID in the SSL_SESSION */625s->session->kex_group = ksgroup;626if ((s->s3.peer_tmp = ssl_generate_param_group(s, ksgroup)) == NULL) {627SSLfatal(s,628SSL_AD_INTERNAL_ERROR,629SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);630return 0;631}632if (tls13_set_encoded_pub_key(s->s3.peer_tmp,633PACKET_data(encoded_pubkey),634PACKET_remaining(encoded_pubkey))635<= 0) {636SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);637return 0;638}639return 1;640}641642#define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (nominally 64 Bytes chunks) */643644typedef enum KS_EXTRACTION_RESULT {645EXTRACTION_FAILURE,646EXTRACTION_SUCCESS,647EXTRACTION_SUCCESS_HRR648} KS_EXTRACTION_RESULT;649650static KS_EXTRACTION_RESULT extract_keyshares(SSL_CONNECTION *s, PACKET *key_share_list,651const uint16_t *clntgroups, size_t clnt_num_groups,652const uint16_t *srvrgroups, size_t srvr_num_groups,653uint16_t **keyshares_arr, PACKET **encoded_pubkey_arr,654size_t *keyshares_cnt, size_t *keyshares_max)655{656PACKET encoded_pubkey;657size_t key_share_pos = 0;658size_t previous_key_share_pos = 0;659unsigned int group_id = 0;660661/* Prepare memory to hold the extracted key share groups and related pubkeys */662*keyshares_arr = OPENSSL_malloc(*keyshares_max * sizeof(**keyshares_arr));663if (*keyshares_arr == NULL) {664SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);665goto failure;666}667*encoded_pubkey_arr = OPENSSL_malloc(*keyshares_max * sizeof(**encoded_pubkey_arr));668if (*encoded_pubkey_arr == NULL) {669SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);670goto failure;671}672673while (PACKET_remaining(key_share_list) > 0) {674/* Get the group_id for the current share and its encoded_pubkey */675if (!PACKET_get_net_2(key_share_list, &group_id)676|| !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey)677|| PACKET_remaining(&encoded_pubkey) == 0) {678SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);679goto failure;680}681682/*683* If we sent an HRR then the key_share sent back MUST be for the group684* we requested, and must be the only key_share sent.685*/686if (s->s3.group_id != 0687&& (group_id != s->s3.group_id688|| PACKET_remaining(key_share_list) != 0)) {689SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);690goto failure;691}692693/*694* Check if this share is in supported_groups sent from client695* RFC 8446 also mandates that clients send keyshares in the same696* order as listed in the supported groups extension, but its not697* required that the server check that, and some clients violate this698* so instead of failing the connection when that occurs, log a trace699* message indicating the client discrepancy.700*/701if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) {702SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);703goto failure;704}705706if (key_share_pos < previous_key_share_pos)707OSSL_TRACE1(TLS, "key share group id %d is out of RFC 8446 order\n", group_id);708709previous_key_share_pos = key_share_pos;710711if (s->s3.group_id != 0) {712/*713* We have sent a HRR, and the key share we got back is714* the one we expected and is the only key share and is715* in the list of supported_groups (checked716* above already), hence we accept this key share group717*/718if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey))719goto failure; /* SSLfatal already called */720/* We have selected a key share group via HRR, hence we're done here */721return EXTRACTION_SUCCESS_HRR;722}723724/*725* We tolerate but ignore a group id that we don't think is726* suitable for TLSv1.3 or which is not supported by the server727*/728if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL)729|| !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)730|| !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,7310, NULL)) {732/* Share not suitable or not supported, check next share */733continue;734}735736/* Memorize this key share group ID and its encoded point */737(*keyshares_arr)[*keyshares_cnt] = group_id;738(*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey;739740/*741* Memory management (remark: While limiting the client to only allow742* a maximum of OPENSSL_CLIENT_MAX_KEY_SHARES to be sent, the server can743* handle any number of key shares)744*/745if (*keyshares_cnt == *keyshares_max) {746PACKET *tmp_pkt;747uint16_t *tmp = OPENSSL_realloc(*keyshares_arr,748(*keyshares_max + GROUPLIST_INCREMENT) * sizeof(**keyshares_arr));749750if (tmp == NULL) {751SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);752goto failure;753}754755*keyshares_arr = tmp;756tmp_pkt = OPENSSL_realloc(*encoded_pubkey_arr,757(*keyshares_max + GROUPLIST_INCREMENT) * sizeof(**encoded_pubkey_arr));758if (tmp_pkt == NULL) {759SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);760goto failure;761}762763*encoded_pubkey_arr = tmp_pkt;764*keyshares_max += GROUPLIST_INCREMENT;765}766}767768return EXTRACTION_SUCCESS;769770failure:771/* Fatal error -> free any allocated memory and return 0 */772OPENSSL_free(*keyshares_arr);773OPENSSL_free(*encoded_pubkey_arr);774return EXTRACTION_FAILURE;775}776#endif777778/*779* For each group in the priority list of groups, check if that group is780* also present in the secondary list; if so, select the first overlap and781* assign to selected_group and also set the related index in the candidate group list,782* or set selected_group to 0 if no overlap783*/784#ifndef OPENSSL_NO_TLS1_3785static void check_overlap(SSL_CONNECTION *s,786const uint16_t *prio_groups, size_t prio_num_groups,787const uint16_t *candidate_groups, size_t candidate_num_groups,788int *prio_group_idx, int *candidate_group_idx,789uint16_t *selected_group)790{791uint16_t current_group;792size_t group_idx = prio_num_groups;793size_t new_group_idx = 0;794795*candidate_group_idx = 0;796*prio_group_idx = 0;797*selected_group = 0;798799for (current_group = 0; current_group < candidate_num_groups; current_group++) {800if (!check_in_list(s, candidate_groups[current_group], prio_groups,801prio_num_groups, 1, &new_group_idx)802|| !tls_group_allowed(s, candidate_groups[current_group],803SSL_SECOP_CURVE_SUPPORTED)804|| !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION,805TLS1_3_VERSION, 0, NULL))806/* No overlap or group not suitable, check next group */807continue;808809/*810* is the found new_group_idx earlier in the priority list than811* initial or last group_idx?812*/813if (new_group_idx < group_idx) {814group_idx = new_group_idx;815*candidate_group_idx = current_group;816*prio_group_idx = group_idx;817*selected_group = prio_groups[group_idx];818}819}820}821#endif822823int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt,824unsigned int context, X509 *x, size_t chainidx)825{826#ifndef OPENSSL_NO_TLS1_3827PACKET key_share_list;828const uint16_t *clntgroups, *srvrgroups;829const size_t *srvrtuples;830uint16_t *first_group_in_tuple;831size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples;832PACKET *encoded_pubkey_arr = NULL;833uint16_t *keyshares_arr = NULL;834size_t keyshares_cnt = 0;835size_t keyshares_max = GROUPLIST_INCREMENT;836/* We conservatively assume that we did not find a suitable group */837uint16_t group_id_candidate = 0;838KS_EXTRACTION_RESULT ks_extraction_result;839size_t current_tuple;840int ret = 0;841842s->s3.group_id_candidate = 0;843if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)844return 1;845846/* Sanity check */847if (s->s3.peer_tmp != NULL) {848SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);849return 0;850}851852if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {853SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);854return 0;855}856857/* Get list of server supported groups and the group tuples */858tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);859tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples);860/* Get the clients list of supported groups. */861tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);862863if (clnt_num_groups == 0) {864/*865* This can only happen if the supported_groups extension was not sent,866* because we verify that the length is non-zero when we process that867* extension.868*/869SSLfatal(s, SSL_AD_MISSING_EXTENSION,870SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);871return 0;872}873874if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) {875/*876* If we set a group_id already, then we must have sent an HRR877* requesting a new key_share. If we haven't got one then that is an878* error879*/880SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);881return 0;882}883884/* We parse the key share extension and memorize the entries (after some checks) */885ks_extraction_result = extract_keyshares(s,886&key_share_list,887clntgroups, clnt_num_groups,888srvrgroups, srvr_num_groups,889&keyshares_arr, &encoded_pubkey_arr,890&keyshares_cnt, &keyshares_max);891892if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */893return 0; /* Memory already freed and SSLfatal already called */894if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */895goto end;896897/*898* We now have the following lists available to make a decision for899* which group the server should use for key exchange :900* From client: clntgroups[clnt_num_groups],901* keyshares_arr[keyshares_cnt], encoded_pubkey_arr[keyshares_cnt]902* From server: srvrgroups[srvr_num_groups], srvrtuples[srvr_num_tuples]903*904* Group selection algorithm:905* For all tuples do:906* key share group(s) overlapping with current tuple?907* --> Yes: accept group_id for SH908* --> No: is any of the client supported_groups overlapping with current tuple?909* --> Yes: memorize group_id for HRR, break910* --> No: continue to check next tuple911*912* Remark: Selection priority different for client- or server-preference913*/914first_group_in_tuple = (uint16_t *)srvrgroups;915for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) {916size_t number_of_groups_in_tuple = srvrtuples[current_tuple];917int prio_group_idx = 0, candidate_group_idx = 0;918919/* Server or client preference ? */920if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {921/* Server preference */922/* Is there overlap with a key share group? */923check_overlap(s,924first_group_in_tuple, number_of_groups_in_tuple,925keyshares_arr, keyshares_cnt,926&prio_group_idx, &candidate_group_idx,927&group_id_candidate);928if (group_id_candidate > 0) { /* Overlap found -> accept the key share group */929if (!tls_accept_ksgroup(s, group_id_candidate,930&encoded_pubkey_arr[candidate_group_idx]))931goto err; /* SSLfatal already called */932/* We have all info for a SH, hence we're done here */933goto end;934} else {935/*936* There's no overlap with a key share, but is there at least a client937* supported_group overlapping with the current tuple?938*/939check_overlap(s,940first_group_in_tuple, number_of_groups_in_tuple,941clntgroups, clnt_num_groups,942&prio_group_idx, &candidate_group_idx,943&group_id_candidate);944if (group_id_candidate > 0) {945/*946* We did not have a key share overlap, but at least the supported947* groups overlap hence we can stop searching948* (and report group_id_candidate 'upward' for HRR)949*/950s->s3.group_id_candidate = group_id_candidate;951goto end;952} else {953/*954* Neither key share nor supported_groups overlap current955* tuple, hence we try the next tuple956*/957first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];958continue;959}960}961962} else { /* We have client preference */963check_overlap(s,964keyshares_arr, keyshares_cnt,965first_group_in_tuple, number_of_groups_in_tuple,966&prio_group_idx, &candidate_group_idx,967&group_id_candidate);968if (group_id_candidate > 0) {969if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx]))970goto err;971goto end;972} else {973check_overlap(s,974clntgroups, clnt_num_groups,975first_group_in_tuple, number_of_groups_in_tuple,976&prio_group_idx, &candidate_group_idx,977&group_id_candidate);978if (group_id_candidate > 0) {979s->s3.group_id_candidate = group_id_candidate;980goto end;981} else {982first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];983continue;984}985}986}987}988989end:990ret = 1;991992err:993OPENSSL_free(keyshares_arr);994OPENSSL_free(encoded_pubkey_arr);995return ret;996997#endif998999return 1;1000}10011002int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,1003X509 *x, size_t chainidx)1004{1005#ifndef OPENSSL_NO_TLS1_31006unsigned int format, version, key_share, group_id;1007EVP_MD_CTX *hctx;1008EVP_PKEY *pkey;1009PACKET cookie, raw, chhash, appcookie;1010WPACKET hrrpkt;1011const unsigned char *data, *mdin, *ciphdata;1012unsigned char hmac[SHA256_DIGEST_LENGTH];1013unsigned char hrr[MAX_HRR_SIZE];1014size_t rawlen, hmaclen, hrrlen, ciphlen;1015uint64_t tm, now;1016SSL *ssl = SSL_CONNECTION_GET_SSL(s);1017SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);10181019/* Ignore any cookie if we're not set up to verify it */1020if (sctx->verify_stateless_cookie_cb == NULL1021|| (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)1022return 1;10231024if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {1025SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1026return 0;1027}10281029raw = cookie;1030data = PACKET_data(&raw);1031rawlen = PACKET_remaining(&raw);1032if (rawlen < SHA256_DIGEST_LENGTH1033|| !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {1034SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1035return 0;1036}1037mdin = PACKET_data(&raw);10381039/* Verify the HMAC of the cookie */1040hctx = EVP_MD_CTX_create();1041pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",1042sctx->propq,1043s->session_ctx->ext.cookie_hmac_key,1044sizeof(s->session_ctx->ext.cookie_hmac_key));1045if (hctx == NULL || pkey == NULL) {1046EVP_MD_CTX_free(hctx);1047EVP_PKEY_free(pkey);1048SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);1049return 0;1050}10511052hmaclen = SHA256_DIGEST_LENGTH;1053if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,1054sctx->propq, pkey, NULL)1055<= 01056|| EVP_DigestSign(hctx, hmac, &hmaclen, data,1057rawlen - SHA256_DIGEST_LENGTH)1058<= 01059|| hmaclen != SHA256_DIGEST_LENGTH) {1060EVP_MD_CTX_free(hctx);1061EVP_PKEY_free(pkey);1062SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1063return 0;1064}10651066EVP_MD_CTX_free(hctx);1067EVP_PKEY_free(pkey);10681069if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {1070SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);1071return 0;1072}10731074if (!PACKET_get_net_2(&cookie, &format)) {1075SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1076return 0;1077}1078/* Check the cookie format is something we recognise. Ignore it if not */1079if (format != COOKIE_STATE_FORMAT_VERSION)1080return 1;10811082/*1083* The rest of these checks really shouldn't fail since we have verified the1084* HMAC above.1085*/10861087/* Check the version number is sane */1088if (!PACKET_get_net_2(&cookie, &version)) {1089SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1090return 0;1091}1092if (version != TLS1_3_VERSION) {1093SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,1094SSL_R_BAD_PROTOCOL_VERSION_NUMBER);1095return 0;1096}10971098if (!PACKET_get_net_2(&cookie, &group_id)) {1099SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1100return 0;1101}11021103ciphdata = PACKET_data(&cookie);1104if (!PACKET_forward(&cookie, 2)) {1105SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1106return 0;1107}1108if (group_id != s->s3.group_id1109|| s->s3.tmp.new_cipher1110!= ssl_get_cipher_by_char(s, ciphdata, 0)) {1111/*1112* We chose a different cipher or group id this time around to what is1113* in the cookie. Something must have changed.1114*/1115SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);1116return 0;1117}11181119if (!PACKET_get_1(&cookie, &key_share)1120|| !PACKET_get_net_8(&cookie, &tm)1121|| !PACKET_get_length_prefixed_2(&cookie, &chhash)1122|| !PACKET_get_length_prefixed_1(&cookie, &appcookie)1123|| PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {1124SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1125return 0;1126}11271128/* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */1129now = time(NULL);1130if (tm > now || (now - tm) > 600) {1131/* Cookie is stale. Ignore it */1132return 1;1133}11341135/* Verify the app cookie */1136if (sctx->verify_stateless_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s),1137PACKET_data(&appcookie),1138PACKET_remaining(&appcookie))1139== 0) {1140SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);1141return 0;1142}11431144/*1145* Reconstruct the HRR that we would have sent in response to the original1146* ClientHello so we can add it to the transcript hash.1147* Note: This won't work with custom HRR extensions1148*/1149if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {1150SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1151return 0;1152}1153if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)1154|| !WPACKET_start_sub_packet_u24(&hrrpkt)1155|| !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)1156|| !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)1157|| !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,1158s->tmp_session_id_len)1159|| !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,1160&ciphlen)1161|| !WPACKET_put_bytes_u8(&hrrpkt, 0)1162|| !WPACKET_start_sub_packet_u16(&hrrpkt)) {1163WPACKET_cleanup(&hrrpkt);1164SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1165return 0;1166}1167if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)1168|| !WPACKET_start_sub_packet_u16(&hrrpkt)1169|| !WPACKET_put_bytes_u16(&hrrpkt, s->version)1170|| !WPACKET_close(&hrrpkt)) {1171WPACKET_cleanup(&hrrpkt);1172SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1173return 0;1174}1175if (key_share) {1176if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)1177|| !WPACKET_start_sub_packet_u16(&hrrpkt)1178|| !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)1179|| !WPACKET_close(&hrrpkt)) {1180WPACKET_cleanup(&hrrpkt);1181SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1182return 0;1183}1184}1185if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)1186|| !WPACKET_start_sub_packet_u16(&hrrpkt)1187|| !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)1188|| !WPACKET_close(&hrrpkt) /* cookie extension */1189|| !WPACKET_close(&hrrpkt) /* extension block */1190|| !WPACKET_close(&hrrpkt) /* message */1191|| !WPACKET_get_total_written(&hrrpkt, &hrrlen)1192|| !WPACKET_finish(&hrrpkt)) {1193WPACKET_cleanup(&hrrpkt);1194SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1195return 0;1196}11971198/* Reconstruct the transcript hash */1199if (!create_synthetic_message_hash(s, PACKET_data(&chhash),1200PACKET_remaining(&chhash), hrr,1201hrrlen)) {1202/* SSLfatal() already called */1203return 0;1204}12051206/* Act as if this ClientHello came after a HelloRetryRequest */1207s->hello_retry_request = SSL_HRR_PENDING;12081209s->ext.cookieok = 1;1210#endif12111212return 1;1213}12141215int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt,1216unsigned int context,1217X509 *x, size_t chainidx)1218{1219PACKET supported_groups_list;12201221/* Each group is 2 bytes and we must have at least 1. */1222if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)1223|| PACKET_remaining(&supported_groups_list) == 01224|| (PACKET_remaining(&supported_groups_list) % 2) != 0) {1225SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1226return 0;1227}12281229if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {1230OPENSSL_free(s->ext.peer_supportedgroups);1231s->ext.peer_supportedgroups = NULL;1232s->ext.peer_supportedgroups_len = 0;1233if (!tls1_save_u16(&supported_groups_list,1234&s->ext.peer_supportedgroups,1235&s->ext.peer_supportedgroups_len)) {1236SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1237return 0;1238}1239}12401241return 1;1242}12431244int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,1245X509 *x, size_t chainidx)1246{1247/* The extension must always be empty */1248if (PACKET_remaining(pkt) != 0) {1249SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1250return 0;1251}12521253if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)1254return 1;12551256s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;12571258return 1;1259}12601261int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,1262X509 *x, size_t chainidx)1263{1264if (PACKET_remaining(pkt) != 0) {1265SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1266return 0;1267}12681269if (s->hello_retry_request != SSL_HRR_NONE) {1270SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);1271return 0;1272}12731274return 1;1275}12761277static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL_CONNECTION *s, PACKET *tick,1278SSL_SESSION **sess)1279{1280SSL_SESSION *tmpsess = NULL;12811282s->ext.ticket_expected = 1;12831284switch (PACKET_remaining(tick)) {1285case 0:1286return SSL_TICKET_EMPTY;12871288case SSL_MAX_SSL_SESSION_ID_LENGTH:1289break;12901291default:1292return SSL_TICKET_NO_DECRYPT;1293}12941295tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),1296SSL_MAX_SSL_SESSION_ID_LENGTH);12971298if (tmpsess == NULL)1299return SSL_TICKET_NO_DECRYPT;13001301*sess = tmpsess;1302return SSL_TICKET_SUCCESS;1303}13041305int tls_parse_ctos_psk(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,1306X509 *x, size_t chainidx)1307{1308PACKET identities, binders, binder;1309size_t binderoffset;1310int hashsize;1311SSL_SESSION *sess = NULL;1312unsigned int id, i, ext = 0;1313const EVP_MD *md = NULL;1314SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);1315SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);13161317/*1318* If we have no PSK kex mode that we recognise then we can't resume so1319* ignore this extension1320*/1321if ((s->ext.psk_kex_mode1322& (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE))1323== 0)1324return 1;13251326if (!PACKET_get_length_prefixed_2(pkt, &identities)) {1327SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1328return 0;1329}13301331s->ext.ticket_expected = 0;1332for (id = 0; PACKET_remaining(&identities) != 0; id++) {1333PACKET identity;1334unsigned long ticket_agel;1335size_t idlen;13361337if (!PACKET_get_length_prefixed_2(&identities, &identity)1338|| !PACKET_get_net_4(&identities, &ticket_agel)) {1339SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1340return 0;1341}13421343idlen = PACKET_remaining(&identity);1344if (s->psk_find_session_cb != NULL1345&& !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen,1346&sess)) {1347SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);1348return 0;1349}13501351#ifndef OPENSSL_NO_PSK1352if (sess == NULL1353&& s->psk_server_callback != NULL1354&& idlen <= PSK_MAX_IDENTITY_LEN) {1355char *pskid = NULL;1356unsigned char pskdata[PSK_MAX_PSK_LEN];1357unsigned int pskdatalen;13581359if (!PACKET_strndup(&identity, &pskid)) {1360SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1361return 0;1362}1363pskdatalen = s->psk_server_callback(ussl, pskid, pskdata,1364sizeof(pskdata));1365OPENSSL_free(pskid);1366if (pskdatalen > PSK_MAX_PSK_LEN) {1367SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1368return 0;1369} else if (pskdatalen > 0) {1370const SSL_CIPHER *cipher;1371const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };13721373/*1374* We found a PSK using an old style callback. We don't know1375* the digest so we default to SHA256 as per the TLSv1.3 spec1376*/1377cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),1378tls13_aes128gcmsha256_id);1379if (cipher == NULL) {1380OPENSSL_cleanse(pskdata, pskdatalen);1381SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1382return 0;1383}13841385sess = SSL_SESSION_new();1386if (sess == NULL1387|| !SSL_SESSION_set1_master_key(sess, pskdata,1388pskdatalen)1389|| !SSL_SESSION_set_cipher(sess, cipher)1390|| !SSL_SESSION_set_protocol_version(sess,1391TLS1_3_VERSION)) {1392OPENSSL_cleanse(pskdata, pskdatalen);1393SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1394goto err;1395}1396OPENSSL_cleanse(pskdata, pskdatalen);1397}1398}1399#endif /* OPENSSL_NO_PSK */14001401if (sess != NULL) {1402/* We found a PSK */1403SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);14041405if (sesstmp == NULL) {1406SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1407goto err;1408}1409SSL_SESSION_free(sess);1410sess = sesstmp;14111412/*1413* We've just been told to use this session for this context so1414* make sure the sid_ctx matches up.1415*/1416memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);1417sess->sid_ctx_length = s->sid_ctx_length;1418ext = 1;1419if (id == 0)1420s->ext.early_data_ok = 1;1421s->ext.ticket_expected = 1;1422} else {1423OSSL_TIME t, age, expire;1424int ret;14251426/*1427* If we are using anti-replay protection then we behave as if1428* SSL_OP_NO_TICKET is set - we are caching tickets anyway so there1429* is no point in using full stateless tickets.1430*/1431if ((s->options & SSL_OP_NO_TICKET) != 01432|| (s->max_early_data > 01433&& (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))1434ret = tls_get_stateful_ticket(s, &identity, &sess);1435else1436ret = tls_decrypt_ticket(s, PACKET_data(&identity),1437PACKET_remaining(&identity), NULL, 0,1438&sess);14391440if (ret == SSL_TICKET_EMPTY) {1441SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1442return 0;1443}14441445if (ret == SSL_TICKET_FATAL_ERR_MALLOC1446|| ret == SSL_TICKET_FATAL_ERR_OTHER) {1447SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1448return 0;1449}1450if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)1451continue;14521453/* Check for replay */1454if (s->max_early_data > 01455&& (s->options & SSL_OP_NO_ANTI_REPLAY) == 01456&& !SSL_CTX_remove_session(s->session_ctx, sess)) {1457SSL_SESSION_free(sess);1458sess = NULL;1459continue;1460}14611462age = ossl_time_subtract(ossl_ms2time(ticket_agel),1463ossl_ms2time(sess->ext.tick_age_add));1464t = ossl_time_subtract(ossl_time_now(), sess->time);14651466/*1467* Although internally we use OSS_TIME which has ns granularity,1468* when SSL_SESSION structures are serialised/deserialised we use1469* second granularity for the sess->time field. Therefore it could1470* appear that the client's ticket age is longer than ours (our1471* ticket age calculation should always be slightly longer than the1472* client's due to the network latency). Therefore we add 1000ms to1473* our age calculation to adjust for rounding errors.1474*/1475expire = ossl_time_add(t, ossl_ms2time(1000));14761477if (id == 01478&& ossl_time_compare(sess->timeout, t) >= 01479&& ossl_time_compare(age, expire) <= 01480&& ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),1481expire)1482>= 0) {1483/*1484* Ticket age is within tolerance and not expired. We allow it1485* for early data1486*/1487s->ext.early_data_ok = 1;1488}1489}14901491md = ssl_md(sctx, sess->cipher->algorithm2);1492if (md == NULL) {1493SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1494goto err;1495}1496if (!EVP_MD_is_a(md,1497EVP_MD_get0_name(ssl_md(sctx,1498s->s3.tmp.new_cipher->algorithm2)))) {1499/* The ciphersuite is not compatible with this session. */1500SSL_SESSION_free(sess);1501sess = NULL;1502s->ext.early_data_ok = 0;1503s->ext.ticket_expected = 0;1504continue;1505}1506break;1507}15081509if (sess == NULL)1510return 1;15111512binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;1513hashsize = EVP_MD_get_size(md);1514if (hashsize <= 0)1515goto err;15161517if (!PACKET_get_length_prefixed_2(pkt, &binders)) {1518SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1519goto err;1520}15211522for (i = 0; i <= id; i++) {1523if (!PACKET_get_length_prefixed_1(&binders, &binder)) {1524SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1525goto err;1526}1527}15281529if (PACKET_remaining(&binder) != (size_t)hashsize) {1530SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1531goto err;1532}1533if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,1534binderoffset, PACKET_data(&binder), NULL, sess, 0,1535ext)1536!= 1) {1537/* SSLfatal() already called */1538goto err;1539}15401541s->ext.tick_identity = id;15421543SSL_SESSION_free(s->session);1544s->session = sess;1545return 1;1546err:1547SSL_SESSION_free(sess);1548return 0;1549}15501551int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,1552ossl_unused unsigned int context,1553ossl_unused X509 *x,1554ossl_unused size_t chainidx)1555{1556if (PACKET_remaining(pkt) != 0) {1557SSLfatal(s, SSL_AD_DECODE_ERROR,1558SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);1559return 0;1560}15611562s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;15631564return 1;1565}15661567/*1568* Add the server's renegotiation binding1569*/1570EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,1571unsigned int context, X509 *x,1572size_t chainidx)1573{1574if (!s->s3.send_connection_binding)1575return EXT_RETURN_NOT_SENT;15761577/* Still add this even if SSL_OP_NO_RENEGOTIATION is set */1578if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)1579|| !WPACKET_start_sub_packet_u16(pkt)1580|| !WPACKET_start_sub_packet_u8(pkt)1581|| !WPACKET_memcpy(pkt, s->s3.previous_client_finished,1582s->s3.previous_client_finished_len)1583|| !WPACKET_memcpy(pkt, s->s3.previous_server_finished,1584s->s3.previous_server_finished_len)1585|| !WPACKET_close(pkt)1586|| !WPACKET_close(pkt)) {1587SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1588return EXT_RETURN_FAIL;1589}15901591return EXT_RETURN_SENT;1592}15931594EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,1595unsigned int context, X509 *x,1596size_t chainidx)1597{1598if (s->servername_done != 1)1599return EXT_RETURN_NOT_SENT;16001601/*1602* Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.1603* We just use the servername from the initial handshake.1604*/1605if (s->hit && !SSL_CONNECTION_IS_TLS13(s))1606return EXT_RETURN_NOT_SENT;16071608if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)1609|| !WPACKET_put_bytes_u16(pkt, 0)) {1610SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1611return EXT_RETURN_FAIL;1612}16131614return EXT_RETURN_SENT;1615}16161617/* Add/include the server's max fragment len extension into ServerHello */1618EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,1619unsigned int context, X509 *x,1620size_t chainidx)1621{1622if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))1623return EXT_RETURN_NOT_SENT;16241625/*-1626* 4 bytes for this extension type and extension length1627* 1 byte for the Max Fragment Length code value.1628*/1629if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)1630|| !WPACKET_start_sub_packet_u16(pkt)1631|| !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)1632|| !WPACKET_close(pkt)) {1633SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1634return EXT_RETURN_FAIL;1635}16361637return EXT_RETURN_SENT;1638}16391640EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,1641unsigned int context, X509 *x,1642size_t chainidx)1643{1644unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;1645unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;1646int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))1647&& (s->ext.peer_ecpointformats != NULL);1648const unsigned char *plist;1649size_t plistlen;16501651if (!using_ecc)1652return EXT_RETURN_NOT_SENT;16531654tls1_get_formatlist(s, &plist, &plistlen);1655if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)1656|| !WPACKET_start_sub_packet_u16(pkt)1657|| !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)1658|| !WPACKET_close(pkt)) {1659SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1660return EXT_RETURN_FAIL;1661}16621663return EXT_RETURN_SENT;1664}16651666EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,1667unsigned int context, X509 *x,1668size_t chainidx)1669{1670const uint16_t *groups;1671size_t numgroups, i, first = 1;1672int version;16731674/* s->s3.group_id is non zero if we accepted a key_share */1675if (s->s3.group_id == 0)1676return EXT_RETURN_NOT_SENT;16771678/* Get our list of supported groups */1679tls1_get_supported_groups(s, &groups, &numgroups);1680if (numgroups == 0) {1681SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1682return EXT_RETURN_FAIL;1683}16841685/* Copy group ID if supported */1686version = SSL_version(SSL_CONNECTION_GET_SSL(s));1687for (i = 0; i < numgroups; i++) {1688uint16_t group = groups[i];16891690if (tls_valid_group(s, group, version, version, 0, NULL)1691&& tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {1692if (first) {1693/*1694* Check if the client is already using our preferred group. If1695* so we don't need to add this extension1696*/1697if (s->s3.group_id == group)1698return EXT_RETURN_NOT_SENT;16991700/* Add extension header */1701if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)1702/* Sub-packet for supported_groups extension */1703|| !WPACKET_start_sub_packet_u16(pkt)1704|| !WPACKET_start_sub_packet_u16(pkt)) {1705SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1706return EXT_RETURN_FAIL;1707}17081709first = 0;1710}1711if (!WPACKET_put_bytes_u16(pkt, group)) {1712SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1713return EXT_RETURN_FAIL;1714}1715}1716}17171718if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {1719SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1720return EXT_RETURN_FAIL;1721}17221723return EXT_RETURN_SENT;1724}17251726EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,1727unsigned int context, X509 *x,1728size_t chainidx)1729{1730if (!s->ext.ticket_expected || !tls_use_ticket(s)) {1731s->ext.ticket_expected = 0;1732return EXT_RETURN_NOT_SENT;1733}17341735if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)1736|| !WPACKET_put_bytes_u16(pkt, 0)) {1737SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1738return EXT_RETURN_FAIL;1739}17401741return EXT_RETURN_SENT;1742}17431744#ifndef OPENSSL_NO_OCSP1745EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,1746unsigned int context, X509 *x,1747size_t chainidx)1748{1749/* We don't currently support this extension inside a CertificateRequest */1750if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)1751return EXT_RETURN_NOT_SENT;17521753if (!s->ext.status_expected)1754return EXT_RETURN_NOT_SENT;17551756if (SSL_CONNECTION_IS_TLS13(s) && chainidx != 0)1757return EXT_RETURN_NOT_SENT;17581759if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)1760|| !WPACKET_start_sub_packet_u16(pkt)) {1761SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1762return EXT_RETURN_FAIL;1763}17641765/*1766* In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we1767* send back an empty extension, with the certificate status appearing as a1768* separate message1769*/1770if (SSL_CONNECTION_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) {1771/* SSLfatal() already called */1772return EXT_RETURN_FAIL;1773}1774if (!WPACKET_close(pkt)) {1775SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1776return EXT_RETURN_FAIL;1777}17781779return EXT_RETURN_SENT;1780}1781#endif17821783#ifndef OPENSSL_NO_NEXTPROTONEG1784EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,1785unsigned int context, X509 *x,1786size_t chainidx)1787{1788const unsigned char *npa;1789unsigned int npalen;1790int ret;1791int npn_seen = s->s3.npn_seen;1792SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);17931794s->s3.npn_seen = 0;1795if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)1796return EXT_RETURN_NOT_SENT;17971798ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,1799&npalen, sctx->ext.npn_advertised_cb_arg);1800if (ret == SSL_TLSEXT_ERR_OK) {1801if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)1802|| !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {1803SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1804return EXT_RETURN_FAIL;1805}1806s->s3.npn_seen = 1;1807return EXT_RETURN_SENT;1808}18091810return EXT_RETURN_NOT_SENT;1811}1812#endif18131814EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,1815X509 *x, size_t chainidx)1816{1817if (s->s3.alpn_selected == NULL)1818return EXT_RETURN_NOT_SENT;18191820if (!WPACKET_put_bytes_u16(pkt,1821TLSEXT_TYPE_application_layer_protocol_negotiation)1822|| !WPACKET_start_sub_packet_u16(pkt)1823|| !WPACKET_start_sub_packet_u16(pkt)1824|| !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,1825s->s3.alpn_selected_len)1826|| !WPACKET_close(pkt)1827|| !WPACKET_close(pkt)) {1828SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1829return EXT_RETURN_FAIL;1830}18311832return EXT_RETURN_SENT;1833}18341835#ifndef OPENSSL_NO_SRTP1836EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,1837unsigned int context, X509 *x,1838size_t chainidx)1839{1840if (s->srtp_profile == NULL)1841return EXT_RETURN_NOT_SENT;18421843if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)1844|| !WPACKET_start_sub_packet_u16(pkt)1845|| !WPACKET_put_bytes_u16(pkt, 2)1846|| !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)1847|| !WPACKET_put_bytes_u8(pkt, 0)1848|| !WPACKET_close(pkt)) {1849SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1850return EXT_RETURN_FAIL;1851}18521853return EXT_RETURN_SENT;1854}1855#endif18561857EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,1858unsigned int context,1859X509 *x, size_t chainidx)1860{1861if (!s->ext.use_etm)1862return EXT_RETURN_NOT_SENT;18631864/*1865* Don't use encrypt_then_mac if AEAD or RC4 might want to disable1866* for other cases too.1867*/1868if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD1869|| s->s3.tmp.new_cipher->algorithm_enc == SSL_RC41870|| s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT1871|| s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT121872|| s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA1873|| s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {1874s->ext.use_etm = 0;1875return EXT_RETURN_NOT_SENT;1876}18771878if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)1879|| !WPACKET_put_bytes_u16(pkt, 0)) {1880SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1881return EXT_RETURN_FAIL;1882}18831884return EXT_RETURN_SENT;1885}18861887EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,1888unsigned int context,1889X509 *x, size_t chainidx)1890{1891if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)1892return EXT_RETURN_NOT_SENT;18931894if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)1895|| !WPACKET_put_bytes_u16(pkt, 0)) {1896SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1897return EXT_RETURN_FAIL;1898}18991900return EXT_RETURN_SENT;1901}19021903EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,1904unsigned int context, X509 *x,1905size_t chainidx)1906{1907if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) {1908SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1909return EXT_RETURN_FAIL;1910}19111912if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)1913|| !WPACKET_start_sub_packet_u16(pkt)1914|| !WPACKET_put_bytes_u16(pkt, s->version)1915|| !WPACKET_close(pkt)) {1916SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1917return EXT_RETURN_FAIL;1918}19191920return EXT_RETURN_SENT;1921}19221923EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,1924unsigned int context, X509 *x,1925size_t chainidx)1926{1927#ifndef OPENSSL_NO_TLS1_31928unsigned char *encoded_pubkey;1929size_t encoded_pubkey_len = 0;1930EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;1931const TLS_GROUP_INFO *ginf = NULL;19321933if (s->hello_retry_request == SSL_HRR_PENDING) {1934if (ckey != NULL) {1935/* Original key_share was acceptable so don't ask for another one */1936return EXT_RETURN_NOT_SENT;1937}1938if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)1939|| !WPACKET_start_sub_packet_u16(pkt)1940|| !WPACKET_put_bytes_u16(pkt, s->s3.group_id)1941|| !WPACKET_close(pkt)) {1942SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1943return EXT_RETURN_FAIL;1944}19451946return EXT_RETURN_SENT;1947}19481949if (ckey == NULL) {1950/* No key_share received from client - must be resuming */1951if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {1952SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1953return EXT_RETURN_FAIL;1954}1955return EXT_RETURN_NOT_SENT;1956}19571958if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {1959/*1960* PSK ('hit') and explicitly not doing DHE. If the client sent the1961* DHE option, we take it by default, except if non-DHE would be1962* preferred by config, but this case would have been handled in1963* tls_parse_ctos_psk_kex_modes().1964*/1965return EXT_RETURN_NOT_SENT;1966}19671968if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)1969|| !WPACKET_start_sub_packet_u16(pkt)1970|| !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {1971SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1972return EXT_RETURN_FAIL;1973}19741975if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),1976s->s3.group_id))1977== NULL) {1978SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1979return EXT_RETURN_FAIL;1980}19811982if (!ginf->is_kem) {1983/* Regular KEX */1984skey = ssl_generate_pkey(s, ckey);1985if (skey == NULL) {1986SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);1987return EXT_RETURN_FAIL;1988}19891990/* Generate encoding of server key */1991encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey);1992if (encoded_pubkey_len == 0) {1993SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);1994EVP_PKEY_free(skey);1995return EXT_RETURN_FAIL;1996}19971998if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len)1999|| !WPACKET_close(pkt)) {2000SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2001EVP_PKEY_free(skey);2002OPENSSL_free(encoded_pubkey);2003return EXT_RETURN_FAIL;2004}2005OPENSSL_free(encoded_pubkey);20062007/*2008* This causes the crypto state to be updated based on the derived keys2009*/2010s->s3.tmp.pkey = skey;2011if (ssl_derive(s, skey, ckey, 1) == 0) {2012/* SSLfatal() already called */2013return EXT_RETURN_FAIL;2014}2015} else {2016/* KEM mode */2017unsigned char *ct = NULL;2018size_t ctlen = 0;20192020/*2021* This does not update the crypto state.2022*2023* The generated pms is stored in `s->s3.tmp.pms` to be later used via2024* ssl_gensecret().2025*/2026if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {2027/* SSLfatal() already called */2028return EXT_RETURN_FAIL;2029}20302031if (ctlen == 0) {2032SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2033OPENSSL_free(ct);2034return EXT_RETURN_FAIL;2035}20362037if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)2038|| !WPACKET_close(pkt)) {2039SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2040OPENSSL_free(ct);2041return EXT_RETURN_FAIL;2042}2043OPENSSL_free(ct);20442045/*2046* This causes the crypto state to be updated based on the generated pms2047*/2048if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {2049/* SSLfatal() already called */2050return EXT_RETURN_FAIL;2051}2052}2053s->s3.did_kex = 1;2054return EXT_RETURN_SENT;2055#else2056return EXT_RETURN_FAIL;2057#endif2058}20592060EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,2061unsigned int context,2062X509 *x, size_t chainidx)2063{2064#ifndef OPENSSL_NO_TLS1_32065unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;2066unsigned char *hmac, *hmac2;2067size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;2068EVP_MD_CTX *hctx;2069EVP_PKEY *pkey;2070int ret = EXT_RETURN_FAIL;2071SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);2072SSL *ssl = SSL_CONNECTION_GET_SSL(s);2073SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);20742075if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)2076return EXT_RETURN_NOT_SENT;20772078if (sctx->gen_stateless_cookie_cb == NULL) {2079SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);2080return EXT_RETURN_FAIL;2081}20822083if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)2084|| !WPACKET_start_sub_packet_u16(pkt)2085|| !WPACKET_start_sub_packet_u16(pkt)2086|| !WPACKET_get_total_written(pkt, &startlen)2087|| !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)2088|| !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)2089|| !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)2090|| !WPACKET_put_bytes_u16(pkt, s->s3.group_id)2091|| !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,2092&ciphlen)2093/* Is there a key_share extension present in this HRR? */2094|| !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)2095|| !WPACKET_put_bytes_u64(pkt, time(NULL))2096|| !WPACKET_start_sub_packet_u16(pkt)2097|| !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {2098SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2099return EXT_RETURN_FAIL;2100}21012102/*2103* Get the hash of the initial ClientHello. ssl_handshake_hash() operates2104* on raw buffers, so we first reserve sufficient bytes (above) and then2105* subsequently allocate them (below)2106*/2107if (!ssl3_digest_cached_records(s, 0)2108|| !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {2109/* SSLfatal() already called */2110return EXT_RETURN_FAIL;2111}21122113if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)2114|| !ossl_assert(hashval1 == hashval2)2115|| !WPACKET_close(pkt)2116|| !WPACKET_start_sub_packet_u8(pkt)2117|| !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {2118SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2119return EXT_RETURN_FAIL;2120}21212122/* Generate the application cookie */2123if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,2124&appcookielen)2125== 0) {2126SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);2127return EXT_RETURN_FAIL;2128}21292130if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)2131|| !ossl_assert(appcookie1 == appcookie2)2132|| !WPACKET_close(pkt)2133|| !WPACKET_get_total_written(pkt, &totcookielen)2134|| !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {2135SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2136return EXT_RETURN_FAIL;2137}2138hmaclen = SHA256_DIGEST_LENGTH;21392140totcookielen -= startlen;2141if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {2142SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2143return EXT_RETURN_FAIL;2144}21452146/* HMAC the cookie */2147hctx = EVP_MD_CTX_create();2148pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",2149sctx->propq,2150s->session_ctx->ext.cookie_hmac_key,2151sizeof(s->session_ctx->ext.cookie_hmac_key));2152if (hctx == NULL || pkey == NULL) {2153SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);2154goto err;2155}21562157if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,2158sctx->propq, pkey, NULL)2159<= 02160|| EVP_DigestSign(hctx, hmac, &hmaclen, cookie,2161totcookielen)2162<= 0) {2163SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2164goto err;2165}21662167if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {2168SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2169goto err;2170}21712172if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)2173|| !ossl_assert(hmac == hmac2)2174|| !ossl_assert(cookie == hmac - totcookielen)2175|| !WPACKET_close(pkt)2176|| !WPACKET_close(pkt)) {2177SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2178goto err;2179}21802181ret = EXT_RETURN_SENT;21822183err:2184EVP_MD_CTX_free(hctx);2185EVP_PKEY_free(pkey);2186return ret;2187#else2188return EXT_RETURN_FAIL;2189#endif2190}21912192EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,2193unsigned int context, X509 *x,2194size_t chainidx)2195{2196const unsigned char cryptopro_ext[36] = {21970xfd, 0xe8, /* 65000 */21980x00, 0x20, /* 32 bytes length */21990x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,22000x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,22010x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,22020x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x172203};22042205if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x802206&& (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)2207|| (SSL_get_options(SSL_CONNECTION_GET_SSL(s))2208& SSL_OP_CRYPTOPRO_TLSEXT_BUG)2209== 0)2210return EXT_RETURN_NOT_SENT;22112212if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {2213SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2214return EXT_RETURN_FAIL;2215}22162217return EXT_RETURN_SENT;2218}22192220EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,2221unsigned int context, X509 *x,2222size_t chainidx)2223{2224if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {2225if (s->max_early_data == 0)2226return EXT_RETURN_NOT_SENT;22272228if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)2229|| !WPACKET_start_sub_packet_u16(pkt)2230|| !WPACKET_put_bytes_u32(pkt, s->max_early_data)2231|| !WPACKET_close(pkt)) {2232SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2233return EXT_RETURN_FAIL;2234}22352236return EXT_RETURN_SENT;2237}22382239if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)2240return EXT_RETURN_NOT_SENT;22412242if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)2243|| !WPACKET_start_sub_packet_u16(pkt)2244|| !WPACKET_close(pkt)) {2245SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2246return EXT_RETURN_FAIL;2247}22482249return EXT_RETURN_SENT;2250}22512252EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,2253unsigned int context,2254X509 *x, size_t chainidx)2255{2256if (!s->hit)2257return EXT_RETURN_NOT_SENT;22582259if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)2260|| !WPACKET_start_sub_packet_u16(pkt)2261|| !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)2262|| !WPACKET_close(pkt)) {2263SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2264return EXT_RETURN_FAIL;2265}22662267return EXT_RETURN_SENT;2268}22692270EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,2271unsigned int context,2272X509 *x, size_t chainidx)2273{2274if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR2275&& (send_certificate_request(sc)2276|| sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) {2277/* Did not receive an acceptable cert type - and doing client auth */2278SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);2279return EXT_RETURN_FAIL;2280}22812282if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) {2283sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2284return EXT_RETURN_NOT_SENT;2285}22862287/*2288* Note: only supposed to send this if we are going to do a cert request,2289* but TLSv1.3 could do a PHA request if the client supports it2290*/2291if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)2292|| sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD2293|| sc->client_cert_type == NULL) {2294/* if we don't send it, reset to TLSEXT_cert_type_x509 */2295sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2296sc->ext.client_cert_type = TLSEXT_cert_type_x509;2297return EXT_RETURN_NOT_SENT;2298}22992300if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)2301|| !WPACKET_start_sub_packet_u16(pkt)2302|| !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)2303|| !WPACKET_close(pkt)) {2304SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2305return EXT_RETURN_FAIL;2306}2307return EXT_RETURN_SENT;2308}23092310/* One of |pref|, |other| is configured and the values are sanitized */2311static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,2312const unsigned char *other, size_t other_len,2313uint8_t *chosen_cert_type)2314{2315size_t i;23162317for (i = 0; i < pref_len; i++) {2318if (memchr(other, pref[i], other_len) != NULL) {2319*chosen_cert_type = pref[i];2320return OSSL_CERT_TYPE_CTOS_GOOD;2321}2322}2323return OSSL_CERT_TYPE_CTOS_ERROR;2324}23252326int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,2327unsigned int context,2328X509 *x, size_t chainidx)2329{2330PACKET supported_cert_types;2331const unsigned char *data;2332size_t len;23332334/* Ignore the extension */2335if (sc->client_cert_type == NULL) {2336sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2337sc->ext.client_cert_type = TLSEXT_cert_type_x509;2338return 1;2339}23402341if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {2342sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;2343SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2344return 0;2345}2346if ((len = PACKET_remaining(&supported_cert_types)) == 0) {2347sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;2348SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2349return 0;2350}2351if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {2352sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;2353SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2354return 0;2355}2356/* client_cert_type: client (peer) has priority */2357sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,2358sc->client_cert_type, sc->client_cert_type_len,2359&sc->ext.client_cert_type);23602361/* Ignore the error until sending - so we can check cert auth*/2362return 1;2363}23642365EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,2366unsigned int context,2367X509 *x, size_t chainidx)2368{2369if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) {2370sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2371return EXT_RETURN_NOT_SENT;2372}2373if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD2374|| sc->server_cert_type == NULL) {2375/* if we don't send it, reset to TLSEXT_cert_type_x509 */2376sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2377sc->ext.server_cert_type = TLSEXT_cert_type_x509;2378return EXT_RETURN_NOT_SENT;2379}23802381if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)2382|| !WPACKET_start_sub_packet_u16(pkt)2383|| !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)2384|| !WPACKET_close(pkt)) {2385SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2386return EXT_RETURN_FAIL;2387}2388return EXT_RETURN_SENT;2389}23902391int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,2392unsigned int context,2393X509 *x, size_t chainidx)2394{2395PACKET supported_cert_types;2396const unsigned char *data;2397size_t len;23982399/* Ignore the extension */2400if (sc->server_cert_type == NULL) {2401sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2402sc->ext.server_cert_type = TLSEXT_cert_type_x509;2403return 1;2404}24052406if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {2407SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2408return 0;2409}24102411if ((len = PACKET_remaining(&supported_cert_types)) == 0) {2412SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2413return 0;2414}2415if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {2416SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2417return 0;2418}2419/* server_cert_type: server (this) has priority */2420sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,2421data, len,2422&sc->ext.server_cert_type);2423if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)2424return 1;24252426/* Did not receive an acceptable cert type */2427SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);2428return 0;2429}243024312432