Path: blob/main/crypto/openssl/ssl/statem/extensions_srvr.c
48266 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 &&268!s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s),269PACKET_data(pkt), PACKET_remaining(pkt),270s->ext.session_ticket_cb_arg)) {271SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);272return 0;273}274275return 1;276}277278int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt,279ossl_unused unsigned int context,280ossl_unused X509 *x,281ossl_unused size_t chainidx)282{283PACKET supported_sig_algs;284285if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)286|| PACKET_remaining(&supported_sig_algs) == 0) {287SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);288return 0;289}290291/*292* We use this routine on both clients and servers, and when clients293* get asked for PHA we need to always save the sigalgs regardless294* of whether it was a resumption or not.295*/296if ((!s->server || (s->server && !s->hit))297&& !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {298SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);299return 0;300}301302return 1;303}304305int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,306unsigned int context, X509 *x, size_t chainidx)307{308PACKET supported_sig_algs;309310if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)311|| PACKET_remaining(&supported_sig_algs) == 0) {312SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);313return 0;314}315316/*317* We use this routine on both clients and servers, and when clients318* get asked for PHA we need to always save the sigalgs regardless319* of whether it was a resumption or not.320*/321if ((!s->server || (s->server && !s->hit))322&& !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {323SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);324return 0;325}326327return 1;328}329330#ifndef OPENSSL_NO_OCSP331int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt,332unsigned int context,333X509 *x, size_t chainidx)334{335PACKET responder_id_list, exts;336337/* We ignore this in a resumption handshake */338if (s->hit)339return 1;340341/* Not defined if we get one of these in a client Certificate */342if (x != NULL)343return 1;344345if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {346SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);347return 0;348}349350if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {351/*352* We don't know what to do with any other type so ignore it.353*/354s->ext.status_type = TLSEXT_STATUSTYPE_nothing;355return 1;356}357358if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {359SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);360return 0;361}362363/*364* We remove any OCSP_RESPIDs from a previous handshake365* to prevent unbounded memory growth - CVE-2016-6304366*/367sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);368if (PACKET_remaining(&responder_id_list) > 0) {369s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();370if (s->ext.ocsp.ids == NULL) {371SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);372return 0;373}374} else {375s->ext.ocsp.ids = NULL;376}377378while (PACKET_remaining(&responder_id_list) > 0) {379OCSP_RESPID *id;380PACKET responder_id;381const unsigned char *id_data;382383if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)384|| PACKET_remaining(&responder_id) == 0) {385SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);386return 0;387}388389id_data = PACKET_data(&responder_id);390id = d2i_OCSP_RESPID(NULL, &id_data,391(int)PACKET_remaining(&responder_id));392if (id == NULL) {393SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);394return 0;395}396397if (id_data != PACKET_end(&responder_id)) {398OCSP_RESPID_free(id);399SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);400401return 0;402}403404if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {405OCSP_RESPID_free(id);406SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);407408return 0;409}410}411412/* Read in request_extensions */413if (!PACKET_as_length_prefixed_2(pkt, &exts)) {414SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);415return 0;416}417418if (PACKET_remaining(&exts) > 0) {419const unsigned char *ext_data = PACKET_data(&exts);420421sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,422X509_EXTENSION_free);423s->ext.ocsp.exts =424d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));425if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {426SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);427return 0;428}429}430431return 1;432}433#endif434435#ifndef OPENSSL_NO_NEXTPROTONEG436int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,437X509 *x, size_t chainidx)438{439/*440* We shouldn't accept this extension on a441* renegotiation.442*/443if (SSL_IS_FIRST_HANDSHAKE(s))444s->s3.npn_seen = 1;445446return 1;447}448#endif449450/*451* Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN452* extension, not including type and length. Returns: 1 on success, 0 on error.453*/454int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,455X509 *x, size_t chainidx)456{457PACKET protocol_list, save_protocol_list, protocol;458459if (!SSL_IS_FIRST_HANDSHAKE(s))460return 1;461462if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)463|| PACKET_remaining(&protocol_list) < 2) {464SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);465return 0;466}467468save_protocol_list = protocol_list;469do {470/* Protocol names can't be empty. */471if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)472|| PACKET_remaining(&protocol) == 0) {473SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);474return 0;475}476} while (PACKET_remaining(&protocol_list) != 0);477478OPENSSL_free(s->s3.alpn_proposed);479s->s3.alpn_proposed = NULL;480s->s3.alpn_proposed_len = 0;481if (!PACKET_memdup(&save_protocol_list,482&s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) {483SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);484return 0;485}486487return 1;488}489490#ifndef OPENSSL_NO_SRTP491int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt,492unsigned int context, X509 *x, size_t chainidx)493{494STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;495unsigned int ct, mki_len, id;496int i, srtp_pref;497PACKET subpkt;498SSL *ssl = SSL_CONNECTION_GET_SSL(s);499500/* Ignore this if we have no SRTP profiles */501if (SSL_get_srtp_profiles(ssl) == NULL)502return 1;503504/* Pull off the length of the cipher suite list and check it is even */505if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0506|| !PACKET_get_sub_packet(pkt, &subpkt, ct)) {507SSLfatal(s, SSL_AD_DECODE_ERROR,508SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);509return 0;510}511512srvr = SSL_get_srtp_profiles(ssl);513s->srtp_profile = NULL;514/* Search all profiles for a match initially */515srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);516517while (PACKET_remaining(&subpkt)) {518if (!PACKET_get_net_2(&subpkt, &id)) {519SSLfatal(s, SSL_AD_DECODE_ERROR,520SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);521return 0;522}523524/*525* Only look for match in profiles of higher preference than526* current match.527* If no profiles have been have been configured then this528* does nothing.529*/530for (i = 0; i < srtp_pref; i++) {531SRTP_PROTECTION_PROFILE *sprof =532sk_SRTP_PROTECTION_PROFILE_value(srvr, i);533534if (sprof->id == id) {535s->srtp_profile = sprof;536srtp_pref = i;537break;538}539}540}541542/* Now extract the MKI value as a sanity check, but discard it for now */543if (!PACKET_get_1(pkt, &mki_len)) {544SSLfatal(s, SSL_AD_DECODE_ERROR,545SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);546return 0;547}548549if (!PACKET_forward(pkt, mki_len)550|| PACKET_remaining(pkt)) {551SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);552return 0;553}554555return 1;556}557#endif558559int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,560X509 *x, size_t chainidx)561{562if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))563s->ext.use_etm = 1;564565return 1;566}567568/*569* Process a psk_kex_modes extension received in the ClientHello. |pkt| contains570* the raw PACKET data for the extension. Returns 1 on success or 0 on failure.571*/572int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt,573unsigned int context,574X509 *x, size_t chainidx)575{576#ifndef OPENSSL_NO_TLS1_3577PACKET psk_kex_modes;578unsigned int mode;579580if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)581|| PACKET_remaining(&psk_kex_modes) == 0) {582SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);583return 0;584}585586while (PACKET_get_1(&psk_kex_modes, &mode)) {587if (mode == TLSEXT_KEX_MODE_KE_DHE)588s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;589else if (mode == TLSEXT_KEX_MODE_KE590&& (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)591s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;592}593594if (((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) != 0)595&& (s->options & SSL_OP_PREFER_NO_DHE_KEX) != 0) {596597/*598* If NO_DHE is supported and preferred, then we only remember this599* mode. DHE PSK will not be used for sure, because in any case where600* it would be supported (i.e. if a key share is present), NO_DHE would601* be supported as well. As the latter is preferred it would be602* chosen. By removing DHE PSK here, we don't have to deal with the603* SSL_OP_PREFER_NO_DHE_KEX option in any other place.604*/605s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE;606}607608#endif609610return 1;611}612613/*614* Use function tls_parse_ctos_key_share with helper functions extract_keyshares,615* check_overlap and tls_accept_ksgroup to parse the key_share extension(s)616* received in the ClientHello and to select the group used of the key exchange617*/618619#ifndef OPENSSL_NO_TLS1_3620/*621* Accept a key share group by setting the related variables in s->s3 and622* by generating a pubkey for this group623*/624static int tls_accept_ksgroup(SSL_CONNECTION *s, uint16_t ksgroup, PACKET *encoded_pubkey)625{626/* Accept the key share group */627s->s3.group_id = ksgroup;628s->s3.group_id_candidate = ksgroup;629/* Cache the selected group ID in the SSL_SESSION */630s->session->kex_group = ksgroup;631if ((s->s3.peer_tmp = ssl_generate_param_group(s, ksgroup)) == NULL) {632SSLfatal(s,633SSL_AD_INTERNAL_ERROR,634SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);635return 0;636}637if (tls13_set_encoded_pub_key(s->s3.peer_tmp,638PACKET_data(encoded_pubkey),639PACKET_remaining(encoded_pubkey)) <= 0) {640SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);641return 0;642}643return 1;644}645646# define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (nominally 64 Bytes chunks) */647648typedef enum KS_EXTRACTION_RESULT {649EXTRACTION_FAILURE,650EXTRACTION_SUCCESS,651EXTRACTION_SUCCESS_HRR652} KS_EXTRACTION_RESULT;653654static KS_EXTRACTION_RESULT extract_keyshares(SSL_CONNECTION *s, PACKET *key_share_list,655const uint16_t *clntgroups, size_t clnt_num_groups,656const uint16_t *srvrgroups, size_t srvr_num_groups,657uint16_t **keyshares_arr, PACKET **encoded_pubkey_arr,658size_t *keyshares_cnt, size_t *keyshares_max)659{660PACKET encoded_pubkey;661size_t key_share_pos = 0;662size_t previous_key_share_pos = 0;663unsigned int group_id = 0;664665/* Prepare memory to hold the extracted key share groups and related pubkeys */666*keyshares_arr = OPENSSL_malloc(*keyshares_max * sizeof(**keyshares_arr));667if (*keyshares_arr == NULL) {668SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);669goto failure;670}671*encoded_pubkey_arr = OPENSSL_malloc(*keyshares_max * sizeof(**encoded_pubkey_arr));672if (*encoded_pubkey_arr == NULL) {673SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);674goto failure;675}676677while (PACKET_remaining(key_share_list) > 0) {678/* Get the group_id for the current share and its encoded_pubkey */679if (!PACKET_get_net_2(key_share_list, &group_id)680|| !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey)681|| PACKET_remaining(&encoded_pubkey) == 0) {682SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);683goto failure;684}685686/*687* If we sent an HRR then the key_share sent back MUST be for the group688* we requested, and must be the only key_share sent.689*/690if (s->s3.group_id != 0691&& (group_id != s->s3.group_id692|| PACKET_remaining(key_share_list) != 0)) {693SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);694goto failure;695}696697/*698* Check if this share is in supported_groups sent from client699* RFC 8446 also mandates that clients send keyshares in the same700* order as listed in the supported groups extension, but its not701* required that the server check that, and some clients violate this702* so instead of failing the connection when that occurs, log a trace703* message indicating the client discrepancy.704*/705if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) {706SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);707goto failure;708}709710if (key_share_pos < previous_key_share_pos)711OSSL_TRACE1(TLS, "key share group id %d is out of RFC 8446 order\n", group_id);712713previous_key_share_pos = key_share_pos;714715if (s->s3.group_id != 0) {716/*717* We have sent a HRR, and the key share we got back is718* the one we expected and is the only key share and is719* in the list of supported_groups (checked720* above already), hence we accept this key share group721*/722if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey))723goto failure; /* SSLfatal already called */724/* We have selected a key share group via HRR, hence we're done here */725return EXTRACTION_SUCCESS_HRR;726}727728/*729* We tolerate but ignore a group id that we don't think is730* suitable for TLSv1.3 or which is not supported by the server731*/732if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL)733|| !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)734|| !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,7350, NULL)) {736/* Share not suitable or not supported, check next share */737continue;738}739740/* Memorize this key share group ID and its encoded point */741(*keyshares_arr)[*keyshares_cnt] = group_id;742(*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey;743744/*745* Memory management (remark: While limiting the client to only allow746* a maximum of OPENSSL_CLIENT_MAX_KEY_SHARES to be sent, the server can747* handle any number of key shares)748*/749if (*keyshares_cnt == *keyshares_max) {750PACKET *tmp_pkt;751uint16_t *tmp =752OPENSSL_realloc(*keyshares_arr,753(*keyshares_max + GROUPLIST_INCREMENT) * sizeof(**keyshares_arr));754755if (tmp == NULL)756goto failure;757*keyshares_arr = tmp;758tmp_pkt =759OPENSSL_realloc(*encoded_pubkey_arr,760(*keyshares_max + GROUPLIST_INCREMENT) *761sizeof(**encoded_pubkey_arr));762if (tmp_pkt == NULL)763goto failure;764*encoded_pubkey_arr = tmp_pkt;765*keyshares_max += GROUPLIST_INCREMENT;766}767768}769770return EXTRACTION_SUCCESS;771772failure:773/* Fatal error -> free any allocated memory and return 0 */774OPENSSL_free(*keyshares_arr);775OPENSSL_free(*encoded_pubkey_arr);776return EXTRACTION_FAILURE;777}778#endif779780/*781* For each group in the priority list of groups, check if that group is782* also present in the secondary list; if so, select the first overlap and783* assign to selected_group and also set the related index in the candidate group list,784* or set selected_group to 0 if no overlap785*/786#ifndef OPENSSL_NO_TLS1_3787static void check_overlap(SSL_CONNECTION *s,788const uint16_t *prio_groups, size_t prio_num_groups,789const uint16_t *candidate_groups, size_t candidate_num_groups,790int *prio_group_idx, int *candidate_group_idx,791uint16_t *selected_group)792{793uint16_t current_group;794size_t group_idx = prio_num_groups;795size_t new_group_idx = 0;796797*candidate_group_idx = 0;798*prio_group_idx = 0;799*selected_group = 0;800801for (current_group = 0; current_group < candidate_num_groups; current_group++) {802if (!check_in_list(s, candidate_groups[current_group], prio_groups,803prio_num_groups, 1, &new_group_idx)804|| !tls_group_allowed(s, candidate_groups[current_group],805SSL_SECOP_CURVE_SUPPORTED)806|| !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION,807TLS1_3_VERSION, 0, NULL))808/* No overlap or group not suitable, check next group */809continue;810811/*812* is the found new_group_idx earlier in the priority list than813* initial or last group_idx?814*/815if (new_group_idx < group_idx) {816group_idx = new_group_idx;817*candidate_group_idx = current_group;818*prio_group_idx = group_idx;819*selected_group = prio_groups[group_idx];820}821}822}823#endif824825int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt,826unsigned int context, X509 *x, size_t chainidx)827{828#ifndef OPENSSL_NO_TLS1_3829PACKET key_share_list;830const uint16_t *clntgroups, *srvrgroups;831const size_t *srvrtuples;832uint16_t *first_group_in_tuple;833size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples;834PACKET *encoded_pubkey_arr = NULL;835uint16_t *keyshares_arr = NULL;836size_t keyshares_cnt = 0;837size_t keyshares_max = GROUPLIST_INCREMENT;838/* We conservatively assume that we did not find a suitable group */839uint16_t group_id_candidate = 0;840KS_EXTRACTION_RESULT ks_extraction_result;841size_t current_tuple;842int ret = 0;843844s->s3.group_id_candidate = 0;845if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)846return 1;847848/* Sanity check */849if (s->s3.peer_tmp != NULL) {850SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);851return 0;852}853854if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {855SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);856return 0;857}858859/* Get list of server supported groups and the group tuples */860tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);861tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples);862/* Get the clients list of supported groups. */863tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);864865if (clnt_num_groups == 0) {866/*867* This can only happen if the supported_groups extension was not sent,868* because we verify that the length is non-zero when we process that869* extension.870*/871SSLfatal(s, SSL_AD_MISSING_EXTENSION,872SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);873return 0;874}875876if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) {877/*878* If we set a group_id already, then we must have sent an HRR879* requesting a new key_share. If we haven't got one then that is an880* error881*/882SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);883return 0;884}885886/* We parse the key share extension and memorize the entries (after some checks) */887ks_extraction_result = extract_keyshares(s,888&key_share_list,889clntgroups, clnt_num_groups,890srvrgroups, srvr_num_groups,891&keyshares_arr, &encoded_pubkey_arr,892&keyshares_cnt, &keyshares_max);893894if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */895return 0; /* Memory already freed and SSLfatal already called */896if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */897goto end;898899/*900* We now have the folowing lists available to make a decision for901* which group the server should use for key exchange :902* From client: clntgroups[clnt_num_groups],903* keyshares_arr[keyshares_cnt], encoded_pubkey_arr[keyshares_cnt]904* From server: srvrgroups[srvr_num_groups], srvrtuples[srvr_num_tuples]905*906* Group selection algorithm:907* For all tuples do:908* key share group(s) overlapping with current tuple?909* --> Yes: accept group_id for SH910* --> No: is any of the client supported_groups overlapping with current tuple?911* --> Yes: memorize group_id for HRR, break912* --> No: continue to check next tuple913*914* Remark: Selection priority different for client- or server-preference915*/916first_group_in_tuple = (uint16_t *)srvrgroups;917for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) {918size_t number_of_groups_in_tuple = srvrtuples[current_tuple];919int prio_group_idx = 0, candidate_group_idx = 0;920921/* Server or client preference ? */922if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {923/* Server preference */924/* Is there overlap with a key share group? */925check_overlap(s,926first_group_in_tuple, number_of_groups_in_tuple,927keyshares_arr, keyshares_cnt,928&prio_group_idx, &candidate_group_idx,929&group_id_candidate);930if (group_id_candidate > 0) { /* Overlap found -> accept the key share group */931if (!tls_accept_ksgroup(s, group_id_candidate,932&encoded_pubkey_arr[candidate_group_idx]))933goto err; /* SSLfatal already called */934/* We have all info for a SH, hence we're done here */935goto end;936} else {937/*938* There's no overlap with a key share, but is there at least a client939* supported_group overlapping with the current tuple?940*/941check_overlap(s,942first_group_in_tuple, number_of_groups_in_tuple,943clntgroups, clnt_num_groups,944&prio_group_idx, &candidate_group_idx,945&group_id_candidate);946if (group_id_candidate > 0) {947/*948* We did not have a key share overlap, but at least the supported949* groups overlap hence we can stop searching950* (and report group_id_candidate 'upward' for HRR)951*/952s->s3.group_id_candidate = group_id_candidate;953goto end;954} else {955/*956* Neither key share nor supported_groups overlap current957* tuple, hence we try the next tuple958*/959first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];960continue;961}962}963964} else { /* We have client preference */965check_overlap(s,966keyshares_arr, keyshares_cnt,967first_group_in_tuple, number_of_groups_in_tuple,968&prio_group_idx, &candidate_group_idx,969&group_id_candidate);970if (group_id_candidate > 0) {971if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx]))972goto err;973goto end;974} else {975check_overlap(s,976clntgroups, clnt_num_groups,977first_group_in_tuple, number_of_groups_in_tuple,978&prio_group_idx, &candidate_group_idx,979&group_id_candidate);980if (group_id_candidate > 0) {981s->s3.group_id_candidate = group_id_candidate;982goto end;983} else {984first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];985continue;986}987}988}989}990991end:992ret = 1;993994err:995OPENSSL_free(keyshares_arr);996OPENSSL_free(encoded_pubkey_arr);997return ret;998999#endif10001001return 1;1002}10031004int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,1005X509 *x, size_t chainidx)1006{1007#ifndef OPENSSL_NO_TLS1_31008unsigned int format, version, key_share, group_id;1009EVP_MD_CTX *hctx;1010EVP_PKEY *pkey;1011PACKET cookie, raw, chhash, appcookie;1012WPACKET hrrpkt;1013const unsigned char *data, *mdin, *ciphdata;1014unsigned char hmac[SHA256_DIGEST_LENGTH];1015unsigned char hrr[MAX_HRR_SIZE];1016size_t rawlen, hmaclen, hrrlen, ciphlen;1017uint64_t tm, now;1018SSL *ssl = SSL_CONNECTION_GET_SSL(s);1019SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);10201021/* Ignore any cookie if we're not set up to verify it */1022if (sctx->verify_stateless_cookie_cb == NULL1023|| (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)1024return 1;10251026if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {1027SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1028return 0;1029}10301031raw = cookie;1032data = PACKET_data(&raw);1033rawlen = PACKET_remaining(&raw);1034if (rawlen < SHA256_DIGEST_LENGTH1035|| !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {1036SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);1037return 0;1038}1039mdin = PACKET_data(&raw);10401041/* Verify the HMAC of the cookie */1042hctx = EVP_MD_CTX_create();1043pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",1044sctx->propq,1045s->session_ctx->ext.cookie_hmac_key,1046sizeof(s->session_ctx->ext.cookie_hmac_key));1047if (hctx == NULL || pkey == NULL) {1048EVP_MD_CTX_free(hctx);1049EVP_PKEY_free(pkey);1050SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);1051return 0;1052}10531054hmaclen = SHA256_DIGEST_LENGTH;1055if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,1056sctx->propq, pkey, NULL) <= 01057|| EVP_DigestSign(hctx, hmac, &hmaclen, data,1058rawlen - SHA256_DIGEST_LENGTH) <= 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)) == 0) {1139SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);1140return 0;1141}11421143/*1144* Reconstruct the HRR that we would have sent in response to the original1145* ClientHello so we can add it to the transcript hash.1146* Note: This won't work with custom HRR extensions1147*/1148if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {1149SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1150return 0;1151}1152if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)1153|| !WPACKET_start_sub_packet_u24(&hrrpkt)1154|| !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)1155|| !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)1156|| !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,1157s->tmp_session_id_len)1158|| !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,1159&ciphlen)1160|| !WPACKET_put_bytes_u8(&hrrpkt, 0)1161|| !WPACKET_start_sub_packet_u16(&hrrpkt)) {1162WPACKET_cleanup(&hrrpkt);1163SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1164return 0;1165}1166if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)1167|| !WPACKET_start_sub_packet_u16(&hrrpkt)1168|| !WPACKET_put_bytes_u16(&hrrpkt, s->version)1169|| !WPACKET_close(&hrrpkt)) {1170WPACKET_cleanup(&hrrpkt);1171SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1172return 0;1173}1174if (key_share) {1175if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)1176|| !WPACKET_start_sub_packet_u16(&hrrpkt)1177|| !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)1178|| !WPACKET_close(&hrrpkt)) {1179WPACKET_cleanup(&hrrpkt);1180SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1181return 0;1182}1183}1184if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)1185|| !WPACKET_start_sub_packet_u16(&hrrpkt)1186|| !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)1187|| !WPACKET_close(&hrrpkt) /* cookie extension */1188|| !WPACKET_close(&hrrpkt) /* extension block */1189|| !WPACKET_close(&hrrpkt) /* message */1190|| !WPACKET_get_total_written(&hrrpkt, &hrrlen)1191|| !WPACKET_finish(&hrrpkt)) {1192WPACKET_cleanup(&hrrpkt);1193SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1194return 0;1195}11961197/* Reconstruct the transcript hash */1198if (!create_synthetic_message_hash(s, PACKET_data(&chhash),1199PACKET_remaining(&chhash), hrr,1200hrrlen)) {1201/* SSLfatal() already called */1202return 0;1203}12041205/* Act as if this ClientHello came after a HelloRetryRequest */1206s->hello_retry_request = SSL_HRR_PENDING;12071208s->ext.cookieok = 1;1209#endif12101211return 1;1212}12131214int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt,1215unsigned int context,1216X509 *x, size_t chainidx)1217{1218PACKET supported_groups_list;12191220/* Each group is 2 bytes and we must have at least 1. */1221if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)1222|| PACKET_remaining(&supported_groups_list) == 01223|| (PACKET_remaining(&supported_groups_list) % 2) != 0) {1224SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1225return 0;1226}12271228if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {1229OPENSSL_free(s->ext.peer_supportedgroups);1230s->ext.peer_supportedgroups = NULL;1231s->ext.peer_supportedgroups_len = 0;1232if (!tls1_save_u16(&supported_groups_list,1233&s->ext.peer_supportedgroups,1234&s->ext.peer_supportedgroups_len)) {1235SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1236return 0;1237}1238}12391240return 1;1241}12421243int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,1244X509 *x, size_t chainidx)1245{1246/* The extension must always be empty */1247if (PACKET_remaining(pkt) != 0) {1248SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1249return 0;1250}12511252if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)1253return 1;12541255s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;12561257return 1;1258}125912601261int 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)) == 0)1323return 1;13241325if (!PACKET_get_length_prefixed_2(pkt, &identities)) {1326SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1327return 0;1328}13291330s->ext.ticket_expected = 0;1331for (id = 0; PACKET_remaining(&identities) != 0; id++) {1332PACKET identity;1333unsigned long ticket_agel;1334size_t idlen;13351336if (!PACKET_get_length_prefixed_2(&identities, &identity)1337|| !PACKET_get_net_4(&identities, &ticket_agel)) {1338SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1339return 0;1340}13411342idlen = PACKET_remaining(&identity);1343if (s->psk_find_session_cb != NULL1344&& !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen,1345&sess)) {1346SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);1347return 0;1348}13491350#ifndef OPENSSL_NO_PSK1351if (sess == NULL1352&& s->psk_server_callback != NULL1353&& idlen <= PSK_MAX_IDENTITY_LEN) {1354char *pskid = NULL;1355unsigned char pskdata[PSK_MAX_PSK_LEN];1356unsigned int pskdatalen;13571358if (!PACKET_strndup(&identity, &pskid)) {1359SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1360return 0;1361}1362pskdatalen = s->psk_server_callback(ussl, pskid, pskdata,1363sizeof(pskdata));1364OPENSSL_free(pskid);1365if (pskdatalen > PSK_MAX_PSK_LEN) {1366SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1367return 0;1368} else if (pskdatalen > 0) {1369const SSL_CIPHER *cipher;1370const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };13711372/*1373* We found a PSK using an old style callback. We don't know1374* the digest so we default to SHA256 as per the TLSv1.3 spec1375*/1376cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),1377tls13_aes128gcmsha256_id);1378if (cipher == NULL) {1379OPENSSL_cleanse(pskdata, pskdatalen);1380SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1381return 0;1382}13831384sess = SSL_SESSION_new();1385if (sess == NULL1386|| !SSL_SESSION_set1_master_key(sess, pskdata,1387pskdatalen)1388|| !SSL_SESSION_set_cipher(sess, cipher)1389|| !SSL_SESSION_set_protocol_version(sess,1390TLS1_3_VERSION)) {1391OPENSSL_cleanse(pskdata, pskdatalen);1392SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1393goto err;1394}1395OPENSSL_cleanse(pskdata, pskdatalen);1396}1397}1398#endif /* OPENSSL_NO_PSK */13991400if (sess != NULL) {1401/* We found a PSK */1402SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);14031404if (sesstmp == NULL) {1405SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1406goto err;1407}1408SSL_SESSION_free(sess);1409sess = sesstmp;14101411/*1412* We've just been told to use this session for this context so1413* make sure the sid_ctx matches up.1414*/1415memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);1416sess->sid_ctx_length = s->sid_ctx_length;1417ext = 1;1418if (id == 0)1419s->ext.early_data_ok = 1;1420s->ext.ticket_expected = 1;1421} else {1422OSSL_TIME t, age, expire;1423int ret;14241425/*1426* If we are using anti-replay protection then we behave as if1427* SSL_OP_NO_TICKET is set - we are caching tickets anyway so there1428* is no point in using full stateless tickets.1429*/1430if ((s->options & SSL_OP_NO_TICKET) != 01431|| (s->max_early_data > 01432&& (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))1433ret = tls_get_stateful_ticket(s, &identity, &sess);1434else1435ret = tls_decrypt_ticket(s, PACKET_data(&identity),1436PACKET_remaining(&identity), NULL, 0,1437&sess);14381439if (ret == SSL_TICKET_EMPTY) {1440SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1441return 0;1442}14431444if (ret == SSL_TICKET_FATAL_ERR_MALLOC1445|| ret == SSL_TICKET_FATAL_ERR_OTHER) {1446SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1447return 0;1448}1449if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)1450continue;14511452/* Check for replay */1453if (s->max_early_data > 01454&& (s->options & SSL_OP_NO_ANTI_REPLAY) == 01455&& !SSL_CTX_remove_session(s->session_ctx, sess)) {1456SSL_SESSION_free(sess);1457sess = NULL;1458continue;1459}14601461age = ossl_time_subtract(ossl_ms2time(ticket_agel),1462ossl_ms2time(sess->ext.tick_age_add));1463t = ossl_time_subtract(ossl_time_now(), sess->time);14641465/*1466* Although internally we use OSS_TIME which has ns granularity,1467* when SSL_SESSION structures are serialised/deserialised we use1468* second granularity for the sess->time field. Therefore it could1469* appear that the client's ticket age is longer than ours (our1470* ticket age calculation should always be slightly longer than the1471* client's due to the network latency). Therefore we add 1000ms to1472* our age calculation to adjust for rounding errors.1473*/1474expire = ossl_time_add(t, ossl_ms2time(1000));14751476if (id == 01477&& ossl_time_compare(sess->timeout, t) >= 01478&& ossl_time_compare(age, expire) <= 01479&& ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),1480expire) >= 0) {1481/*1482* Ticket age is within tolerance and not expired. We allow it1483* for early data1484*/1485s->ext.early_data_ok = 1;1486}1487}14881489md = ssl_md(sctx, sess->cipher->algorithm2);1490if (md == NULL) {1491SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1492goto err;1493}1494if (!EVP_MD_is_a(md,1495EVP_MD_get0_name(ssl_md(sctx,1496s->s3.tmp.new_cipher->algorithm2)))) {1497/* The ciphersuite is not compatible with this session. */1498SSL_SESSION_free(sess);1499sess = NULL;1500s->ext.early_data_ok = 0;1501s->ext.ticket_expected = 0;1502continue;1503}1504break;1505}15061507if (sess == NULL)1508return 1;15091510binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;1511hashsize = EVP_MD_get_size(md);1512if (hashsize <= 0)1513goto err;15141515if (!PACKET_get_length_prefixed_2(pkt, &binders)) {1516SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1517goto err;1518}15191520for (i = 0; i <= id; i++) {1521if (!PACKET_get_length_prefixed_1(&binders, &binder)) {1522SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1523goto err;1524}1525}15261527if (PACKET_remaining(&binder) != (size_t)hashsize) {1528SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);1529goto err;1530}1531if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,1532binderoffset, PACKET_data(&binder), NULL, sess, 0,1533ext) != 1) {1534/* SSLfatal() already called */1535goto err;1536}15371538s->ext.tick_identity = id;15391540SSL_SESSION_free(s->session);1541s->session = sess;1542return 1;1543err:1544SSL_SESSION_free(sess);1545return 0;1546}15471548int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,1549ossl_unused unsigned int context,1550ossl_unused X509 *x,1551ossl_unused size_t chainidx)1552{1553if (PACKET_remaining(pkt) != 0) {1554SSLfatal(s, SSL_AD_DECODE_ERROR,1555SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);1556return 0;1557}15581559s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;15601561return 1;1562}15631564/*1565* Add the server's renegotiation binding1566*/1567EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,1568unsigned int context, X509 *x,1569size_t chainidx)1570{1571if (!s->s3.send_connection_binding)1572return EXT_RETURN_NOT_SENT;15731574/* Still add this even if SSL_OP_NO_RENEGOTIATION is set */1575if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)1576|| !WPACKET_start_sub_packet_u16(pkt)1577|| !WPACKET_start_sub_packet_u8(pkt)1578|| !WPACKET_memcpy(pkt, s->s3.previous_client_finished,1579s->s3.previous_client_finished_len)1580|| !WPACKET_memcpy(pkt, s->s3.previous_server_finished,1581s->s3.previous_server_finished_len)1582|| !WPACKET_close(pkt)1583|| !WPACKET_close(pkt)) {1584SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1585return EXT_RETURN_FAIL;1586}15871588return EXT_RETURN_SENT;1589}15901591EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,1592unsigned int context, X509 *x,1593size_t chainidx)1594{1595if (s->servername_done != 1)1596return EXT_RETURN_NOT_SENT;15971598/*1599* Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.1600* We just use the servername from the initial handshake.1601*/1602if (s->hit && !SSL_CONNECTION_IS_TLS13(s))1603return EXT_RETURN_NOT_SENT;16041605if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)1606|| !WPACKET_put_bytes_u16(pkt, 0)) {1607SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1608return EXT_RETURN_FAIL;1609}16101611return EXT_RETURN_SENT;1612}16131614/* Add/include the server's max fragment len extension into ServerHello */1615EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,1616unsigned int context, X509 *x,1617size_t chainidx)1618{1619if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))1620return EXT_RETURN_NOT_SENT;16211622/*-1623* 4 bytes for this extension type and extension length1624* 1 byte for the Max Fragment Length code value.1625*/1626if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)1627|| !WPACKET_start_sub_packet_u16(pkt)1628|| !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)1629|| !WPACKET_close(pkt)) {1630SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1631return EXT_RETURN_FAIL;1632}16331634return EXT_RETURN_SENT;1635}16361637EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,1638unsigned int context, X509 *x,1639size_t chainidx)1640{1641unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;1642unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;1643int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))1644&& (s->ext.peer_ecpointformats != NULL);1645const unsigned char *plist;1646size_t plistlen;16471648if (!using_ecc)1649return EXT_RETURN_NOT_SENT;16501651tls1_get_formatlist(s, &plist, &plistlen);1652if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)1653|| !WPACKET_start_sub_packet_u16(pkt)1654|| !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)1655|| !WPACKET_close(pkt)) {1656SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1657return EXT_RETURN_FAIL;1658}16591660return EXT_RETURN_SENT;1661}16621663EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,1664unsigned int context, X509 *x,1665size_t chainidx)1666{1667const uint16_t *groups;1668size_t numgroups, i, first = 1;1669int version;16701671/* s->s3.group_id is non zero if we accepted a key_share */1672if (s->s3.group_id == 0)1673return EXT_RETURN_NOT_SENT;16741675/* Get our list of supported groups */1676tls1_get_supported_groups(s, &groups, &numgroups);1677if (numgroups == 0) {1678SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1679return EXT_RETURN_FAIL;1680}16811682/* Copy group ID if supported */1683version = SSL_version(SSL_CONNECTION_GET_SSL(s));1684for (i = 0; i < numgroups; i++) {1685uint16_t group = groups[i];16861687if (tls_valid_group(s, group, version, version, 0, NULL)1688&& tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {1689if (first) {1690/*1691* Check if the client is already using our preferred group. If1692* so we don't need to add this extension1693*/1694if (s->s3.group_id == group)1695return EXT_RETURN_NOT_SENT;16961697/* Add extension header */1698if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)1699/* Sub-packet for supported_groups extension */1700|| !WPACKET_start_sub_packet_u16(pkt)1701|| !WPACKET_start_sub_packet_u16(pkt)) {1702SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1703return EXT_RETURN_FAIL;1704}17051706first = 0;1707}1708if (!WPACKET_put_bytes_u16(pkt, group)) {1709SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1710return EXT_RETURN_FAIL;1711}1712}1713}17141715if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {1716SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1717return EXT_RETURN_FAIL;1718}17191720return EXT_RETURN_SENT;1721}17221723EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,1724unsigned int context, X509 *x,1725size_t chainidx)1726{1727if (!s->ext.ticket_expected || !tls_use_ticket(s)) {1728s->ext.ticket_expected = 0;1729return EXT_RETURN_NOT_SENT;1730}17311732if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)1733|| !WPACKET_put_bytes_u16(pkt, 0)) {1734SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1735return EXT_RETURN_FAIL;1736}17371738return EXT_RETURN_SENT;1739}17401741#ifndef OPENSSL_NO_OCSP1742EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,1743unsigned int context, X509 *x,1744size_t chainidx)1745{1746/* We don't currently support this extension inside a CertificateRequest */1747if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)1748return EXT_RETURN_NOT_SENT;17491750if (!s->ext.status_expected)1751return EXT_RETURN_NOT_SENT;17521753if (SSL_CONNECTION_IS_TLS13(s) && chainidx != 0)1754return EXT_RETURN_NOT_SENT;17551756if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)1757|| !WPACKET_start_sub_packet_u16(pkt)) {1758SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1759return EXT_RETURN_FAIL;1760}17611762/*1763* In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we1764* send back an empty extension, with the certificate status appearing as a1765* separate message1766*/1767if (SSL_CONNECTION_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) {1768/* SSLfatal() already called */1769return EXT_RETURN_FAIL;1770}1771if (!WPACKET_close(pkt)) {1772SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1773return EXT_RETURN_FAIL;1774}17751776return EXT_RETURN_SENT;1777}1778#endif17791780#ifndef OPENSSL_NO_NEXTPROTONEG1781EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,1782unsigned int context, X509 *x,1783size_t chainidx)1784{1785const unsigned char *npa;1786unsigned int npalen;1787int ret;1788int npn_seen = s->s3.npn_seen;1789SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);17901791s->s3.npn_seen = 0;1792if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)1793return EXT_RETURN_NOT_SENT;17941795ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,1796&npalen, sctx->ext.npn_advertised_cb_arg);1797if (ret == SSL_TLSEXT_ERR_OK) {1798if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)1799|| !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {1800SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1801return EXT_RETURN_FAIL;1802}1803s->s3.npn_seen = 1;1804return EXT_RETURN_SENT;1805}18061807return EXT_RETURN_NOT_SENT;1808}1809#endif18101811EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,1812X509 *x, size_t chainidx)1813{1814if (s->s3.alpn_selected == NULL)1815return EXT_RETURN_NOT_SENT;18161817if (!WPACKET_put_bytes_u16(pkt,1818TLSEXT_TYPE_application_layer_protocol_negotiation)1819|| !WPACKET_start_sub_packet_u16(pkt)1820|| !WPACKET_start_sub_packet_u16(pkt)1821|| !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,1822s->s3.alpn_selected_len)1823|| !WPACKET_close(pkt)1824|| !WPACKET_close(pkt)) {1825SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1826return EXT_RETURN_FAIL;1827}18281829return EXT_RETURN_SENT;1830}18311832#ifndef OPENSSL_NO_SRTP1833EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,1834unsigned int context, X509 *x,1835size_t chainidx)1836{1837if (s->srtp_profile == NULL)1838return EXT_RETURN_NOT_SENT;18391840if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)1841|| !WPACKET_start_sub_packet_u16(pkt)1842|| !WPACKET_put_bytes_u16(pkt, 2)1843|| !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)1844|| !WPACKET_put_bytes_u8(pkt, 0)1845|| !WPACKET_close(pkt)) {1846SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1847return EXT_RETURN_FAIL;1848}18491850return EXT_RETURN_SENT;1851}1852#endif18531854EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,1855unsigned int context,1856X509 *x, size_t chainidx)1857{1858if (!s->ext.use_etm)1859return EXT_RETURN_NOT_SENT;18601861/*1862* Don't use encrypt_then_mac if AEAD or RC4 might want to disable1863* for other cases too.1864*/1865if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD1866|| s->s3.tmp.new_cipher->algorithm_enc == SSL_RC41867|| s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT1868|| s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT121869|| s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA1870|| s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {1871s->ext.use_etm = 0;1872return EXT_RETURN_NOT_SENT;1873}18741875if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)1876|| !WPACKET_put_bytes_u16(pkt, 0)) {1877SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1878return EXT_RETURN_FAIL;1879}18801881return EXT_RETURN_SENT;1882}18831884EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,1885unsigned int context,1886X509 *x, size_t chainidx)1887{1888if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)1889return EXT_RETURN_NOT_SENT;18901891if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)1892|| !WPACKET_put_bytes_u16(pkt, 0)) {1893SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1894return EXT_RETURN_FAIL;1895}18961897return EXT_RETURN_SENT;1898}18991900EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,1901unsigned int context, X509 *x,1902size_t chainidx)1903{1904if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) {1905SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1906return EXT_RETURN_FAIL;1907}19081909if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)1910|| !WPACKET_start_sub_packet_u16(pkt)1911|| !WPACKET_put_bytes_u16(pkt, s->version)1912|| !WPACKET_close(pkt)) {1913SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1914return EXT_RETURN_FAIL;1915}19161917return EXT_RETURN_SENT;1918}19191920EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,1921unsigned int context, X509 *x,1922size_t chainidx)1923{1924#ifndef OPENSSL_NO_TLS1_31925unsigned char *encoded_pubkey;1926size_t encoded_pubkey_len = 0;1927EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;1928const TLS_GROUP_INFO *ginf = NULL;19291930if (s->hello_retry_request == SSL_HRR_PENDING) {1931if (ckey != NULL) {1932/* Original key_share was acceptable so don't ask for another one */1933return EXT_RETURN_NOT_SENT;1934}1935if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)1936|| !WPACKET_start_sub_packet_u16(pkt)1937|| !WPACKET_put_bytes_u16(pkt, s->s3.group_id)1938|| !WPACKET_close(pkt)) {1939SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1940return EXT_RETURN_FAIL;1941}19421943return EXT_RETURN_SENT;1944}19451946if (ckey == NULL) {1947/* No key_share received from client - must be resuming */1948if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {1949SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1950return EXT_RETURN_FAIL;1951}1952return EXT_RETURN_NOT_SENT;1953}19541955if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {1956/*1957* PSK ('hit') and explicitly not doing DHE. If the client sent the1958* DHE option, we take it by default, except if non-DHE would be1959* preferred by config, but this case would have been handled in1960* tls_parse_ctos_psk_kex_modes().1961*/1962return EXT_RETURN_NOT_SENT;1963}19641965if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)1966|| !WPACKET_start_sub_packet_u16(pkt)1967|| !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {1968SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1969return EXT_RETURN_FAIL;1970}19711972if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),1973s->s3.group_id)) == NULL) {1974SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1975return EXT_RETURN_FAIL;1976}19771978if (!ginf->is_kem) {1979/* Regular KEX */1980skey = ssl_generate_pkey(s, ckey);1981if (skey == NULL) {1982SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);1983return EXT_RETURN_FAIL;1984}19851986/* Generate encoding of server key */1987encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey);1988if (encoded_pubkey_len == 0) {1989SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);1990EVP_PKEY_free(skey);1991return EXT_RETURN_FAIL;1992}19931994if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len)1995|| !WPACKET_close(pkt)) {1996SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);1997EVP_PKEY_free(skey);1998OPENSSL_free(encoded_pubkey);1999return EXT_RETURN_FAIL;2000}2001OPENSSL_free(encoded_pubkey);20022003/*2004* This causes the crypto state to be updated based on the derived keys2005*/2006s->s3.tmp.pkey = skey;2007if (ssl_derive(s, skey, ckey, 1) == 0) {2008/* SSLfatal() already called */2009return EXT_RETURN_FAIL;2010}2011} else {2012/* KEM mode */2013unsigned char *ct = NULL;2014size_t ctlen = 0;20152016/*2017* This does not update the crypto state.2018*2019* The generated pms is stored in `s->s3.tmp.pms` to be later used via2020* ssl_gensecret().2021*/2022if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {2023/* SSLfatal() already called */2024return EXT_RETURN_FAIL;2025}20262027if (ctlen == 0) {2028SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2029OPENSSL_free(ct);2030return EXT_RETURN_FAIL;2031}20322033if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)2034|| !WPACKET_close(pkt)) {2035SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2036OPENSSL_free(ct);2037return EXT_RETURN_FAIL;2038}2039OPENSSL_free(ct);20402041/*2042* This causes the crypto state to be updated based on the generated pms2043*/2044if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {2045/* SSLfatal() already called */2046return EXT_RETURN_FAIL;2047}2048}2049s->s3.did_kex = 1;2050return EXT_RETURN_SENT;2051#else2052return EXT_RETURN_FAIL;2053#endif2054}20552056EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,2057unsigned int context,2058X509 *x, size_t chainidx)2059{2060#ifndef OPENSSL_NO_TLS1_32061unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;2062unsigned char *hmac, *hmac2;2063size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;2064EVP_MD_CTX *hctx;2065EVP_PKEY *pkey;2066int ret = EXT_RETURN_FAIL;2067SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);2068SSL *ssl = SSL_CONNECTION_GET_SSL(s);2069SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);20702071if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)2072return EXT_RETURN_NOT_SENT;20732074if (sctx->gen_stateless_cookie_cb == NULL) {2075SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);2076return EXT_RETURN_FAIL;2077}20782079if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)2080|| !WPACKET_start_sub_packet_u16(pkt)2081|| !WPACKET_start_sub_packet_u16(pkt)2082|| !WPACKET_get_total_written(pkt, &startlen)2083|| !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)2084|| !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)2085|| !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)2086|| !WPACKET_put_bytes_u16(pkt, s->s3.group_id)2087|| !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,2088&ciphlen)2089/* Is there a key_share extension present in this HRR? */2090|| !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)2091|| !WPACKET_put_bytes_u64(pkt, time(NULL))2092|| !WPACKET_start_sub_packet_u16(pkt)2093|| !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {2094SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2095return EXT_RETURN_FAIL;2096}20972098/*2099* Get the hash of the initial ClientHello. ssl_handshake_hash() operates2100* on raw buffers, so we first reserve sufficient bytes (above) and then2101* subsequently allocate them (below)2102*/2103if (!ssl3_digest_cached_records(s, 0)2104|| !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {2105/* SSLfatal() already called */2106return EXT_RETURN_FAIL;2107}21082109if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)2110|| !ossl_assert(hashval1 == hashval2)2111|| !WPACKET_close(pkt)2112|| !WPACKET_start_sub_packet_u8(pkt)2113|| !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {2114SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2115return EXT_RETURN_FAIL;2116}21172118/* Generate the application cookie */2119if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,2120&appcookielen) == 0) {2121SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);2122return EXT_RETURN_FAIL;2123}21242125if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)2126|| !ossl_assert(appcookie1 == appcookie2)2127|| !WPACKET_close(pkt)2128|| !WPACKET_get_total_written(pkt, &totcookielen)2129|| !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {2130SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2131return EXT_RETURN_FAIL;2132}2133hmaclen = SHA256_DIGEST_LENGTH;21342135totcookielen -= startlen;2136if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {2137SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2138return EXT_RETURN_FAIL;2139}21402141/* HMAC the cookie */2142hctx = EVP_MD_CTX_create();2143pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",2144sctx->propq,2145s->session_ctx->ext.cookie_hmac_key,2146sizeof(s->session_ctx->ext.cookie_hmac_key));2147if (hctx == NULL || pkey == NULL) {2148SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);2149goto err;2150}21512152if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,2153sctx->propq, pkey, NULL) <= 02154|| EVP_DigestSign(hctx, hmac, &hmaclen, cookie,2155totcookielen) <= 0) {2156SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2157goto err;2158}21592160if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {2161SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2162goto err;2163}21642165if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)2166|| !ossl_assert(hmac == hmac2)2167|| !ossl_assert(cookie == hmac - totcookielen)2168|| !WPACKET_close(pkt)2169|| !WPACKET_close(pkt)) {2170SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2171goto err;2172}21732174ret = EXT_RETURN_SENT;21752176err:2177EVP_MD_CTX_free(hctx);2178EVP_PKEY_free(pkey);2179return ret;2180#else2181return EXT_RETURN_FAIL;2182#endif2183}21842185EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,2186unsigned int context, X509 *x,2187size_t chainidx)2188{2189const unsigned char cryptopro_ext[36] = {21900xfd, 0xe8, /* 65000 */21910x00, 0x20, /* 32 bytes length */21920x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,21930x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,21940x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,21950x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x172196};21972198if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x802199&& (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)2200|| (SSL_get_options(SSL_CONNECTION_GET_SSL(s))2201& SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)2202return EXT_RETURN_NOT_SENT;22032204if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {2205SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2206return EXT_RETURN_FAIL;2207}22082209return EXT_RETURN_SENT;2210}22112212EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,2213unsigned int context, X509 *x,2214size_t chainidx)2215{2216if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {2217if (s->max_early_data == 0)2218return EXT_RETURN_NOT_SENT;22192220if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)2221|| !WPACKET_start_sub_packet_u16(pkt)2222|| !WPACKET_put_bytes_u32(pkt, s->max_early_data)2223|| !WPACKET_close(pkt)) {2224SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2225return EXT_RETURN_FAIL;2226}22272228return EXT_RETURN_SENT;2229}22302231if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)2232return EXT_RETURN_NOT_SENT;22332234if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)2235|| !WPACKET_start_sub_packet_u16(pkt)2236|| !WPACKET_close(pkt)) {2237SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2238return EXT_RETURN_FAIL;2239}22402241return EXT_RETURN_SENT;2242}22432244EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,2245unsigned int context,2246X509 *x, size_t chainidx)2247{2248if (!s->hit)2249return EXT_RETURN_NOT_SENT;22502251if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)2252|| !WPACKET_start_sub_packet_u16(pkt)2253|| !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)2254|| !WPACKET_close(pkt)) {2255SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2256return EXT_RETURN_FAIL;2257}22582259return EXT_RETURN_SENT;2260}22612262EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,2263unsigned int context,2264X509 *x, size_t chainidx)2265{2266if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR2267&& (send_certificate_request(sc)2268|| sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) {2269/* Did not receive an acceptable cert type - and doing client auth */2270SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);2271return EXT_RETURN_FAIL;2272}22732274if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) {2275sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2276return EXT_RETURN_NOT_SENT;2277}22782279/*2280* Note: only supposed to send this if we are going to do a cert request,2281* but TLSv1.3 could do a PHA request if the client supports it2282*/2283if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)2284|| sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD2285|| sc->client_cert_type == NULL) {2286/* if we don't send it, reset to TLSEXT_cert_type_x509 */2287sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2288sc->ext.client_cert_type = TLSEXT_cert_type_x509;2289return EXT_RETURN_NOT_SENT;2290}22912292if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)2293|| !WPACKET_start_sub_packet_u16(pkt)2294|| !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)2295|| !WPACKET_close(pkt)) {2296SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2297return EXT_RETURN_FAIL;2298}2299return EXT_RETURN_SENT;2300}23012302/* One of |pref|, |other| is configured and the values are sanitized */2303static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,2304const unsigned char *other, size_t other_len,2305uint8_t *chosen_cert_type)2306{2307size_t i;23082309for (i = 0; i < pref_len; i++) {2310if (memchr(other, pref[i], other_len) != NULL) {2311*chosen_cert_type = pref[i];2312return OSSL_CERT_TYPE_CTOS_GOOD;2313}2314}2315return OSSL_CERT_TYPE_CTOS_ERROR;2316}23172318int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,2319unsigned int context,2320X509 *x, size_t chainidx)2321{2322PACKET supported_cert_types;2323const unsigned char *data;2324size_t len;23252326/* Ignore the extension */2327if (sc->client_cert_type == NULL) {2328sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2329sc->ext.client_cert_type = TLSEXT_cert_type_x509;2330return 1;2331}23322333if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {2334sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;2335SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2336return 0;2337}2338if ((len = PACKET_remaining(&supported_cert_types)) == 0) {2339sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;2340SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2341return 0;2342}2343if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {2344sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;2345SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2346return 0;2347}2348/* client_cert_type: client (peer) has priority */2349sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,2350sc->client_cert_type, sc->client_cert_type_len,2351&sc->ext.client_cert_type);23522353/* Ignore the error until sending - so we can check cert auth*/2354return 1;2355}23562357EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,2358unsigned int context,2359X509 *x, size_t chainidx)2360{2361if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) {2362sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2363return EXT_RETURN_NOT_SENT;2364}2365if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD2366|| sc->server_cert_type == NULL) {2367/* if we don't send it, reset to TLSEXT_cert_type_x509 */2368sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2369sc->ext.server_cert_type = TLSEXT_cert_type_x509;2370return EXT_RETURN_NOT_SENT;2371}23722373if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)2374|| !WPACKET_start_sub_packet_u16(pkt)2375|| !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)2376|| !WPACKET_close(pkt)) {2377SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);2378return EXT_RETURN_FAIL;2379}2380return EXT_RETURN_SENT;2381}23822383int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,2384unsigned int context,2385X509 *x, size_t chainidx)2386{2387PACKET supported_cert_types;2388const unsigned char *data;2389size_t len;23902391/* Ignore the extension */2392if (sc->server_cert_type == NULL) {2393sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;2394sc->ext.server_cert_type = TLSEXT_cert_type_x509;2395return 1;2396}23972398if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {2399SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2400return 0;2401}24022403if ((len = PACKET_remaining(&supported_cert_types)) == 0) {2404SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2405return 0;2406}2407if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {2408SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);2409return 0;2410}2411/* server_cert_type: server (this) has priority */2412sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,2413data, len,2414&sc->ext.server_cert_type);2415if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)2416return 1;24172418/* Did not receive an acceptable cert type */2419SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);2420return 0;2421}242224232424