@@ -261,6 +261,18 @@ bool OSSLContext::set_hostname(std::string_view hostname) const {
261261 const_cast <char *>(name.c_str ())) == 1 ;
262262}
263263
264+ bool OSSLContext::set_verify_hostname (std::string_view hostname) const {
265+ // SSL_set1_host tells OpenSSL to verify the peer certificate's
266+ // subject name (SAN/CN) matches this hostname. This is separate
267+ // from SSL_set_tlsext_host_name which only sets the SNI extension.
268+ static const char * kDefaultHostname = " localhost" ;
269+ if (hostname.empty ()) {
270+ return SSL_set1_host (*this , kDefaultHostname ) == 1 ;
271+ } else {
272+ return SSL_set1_host (*this , hostname.data ()) == 1 ;
273+ }
274+ }
275+
264276bool OSSLContext::set_early_data_enabled () const {
265277 return SSL_set_quic_tls_early_data_enabled (*this , 1 ) == 1 ;
266278}
@@ -714,12 +726,13 @@ Maybe<TLSContext::Options> TLSContext::Options::From(Environment* env,
714726 env, &options, params, state.name ##_string ())
715727
716728 if (!SET (verify_client) || !SET (reject_unauthorized) ||
717- !SET (verify_peer_strict) || !SET (enable_early_data) ||
718- !SET (enable_tls_trace) || !SET (alpn) || !SET (servername) ||
719- !SET (ciphers) || !SET (groups) || !SET (verify_private_key) ||
720- !SET (keylog) || !SET (port) || !SET (authoritative) ||
721- !SET_VECTOR (crypto::KeyObjectData, keys) || !SET_VECTOR (Store, certs) ||
722- !SET_VECTOR (Store, ca) || !SET_VECTOR (Store, crl)) {
729+ !SET (verify_hostname) || !SET (verify_peer_strict) ||
730+ !SET (enable_early_data) || !SET (enable_tls_trace) || !SET (alpn) ||
731+ !SET (servername) || !SET (ciphers) || !SET (groups) ||
732+ !SET (verify_private_key) || !SET (keylog) || !SET (port) ||
733+ !SET (authoritative) || !SET_VECTOR (crypto::KeyObjectData, keys) ||
734+ !SET_VECTOR (Store, certs) || !SET_VECTOR (Store, ca) ||
735+ !SET_VECTOR (Store, crl)) {
723736 return Nothing<Options>();
724737 }
725738
@@ -854,6 +867,14 @@ void TLSSession::Initialize(
854867 return ;
855868 }
856869
870+ if (options.verify_hostname ) {
871+ if (!ossl_context_.set_verify_hostname (options.servername )) {
872+ validation_error_ = " Failed to set verify hostname" ;
873+ ossl_context_.reset ();
874+ return ;
875+ }
876+ }
877+
857878 if (maybeSessionTicket.has_value ()) {
858879 const auto & sessionTicket = *maybeSessionTicket;
859880 uv_buf_t buf = sessionTicket.ticket ();
0 commit comments