@@ -105,6 +105,12 @@ SSL_SESSION* GetSessionCallback(
105105 return w->ReleaseSession ();
106106}
107107
108+ // The server name is handled in EarlyClientHelloCallback, so this only
109+ // acknowledges it, which also has the TLS library store it in new sessions.
110+ int AcknowledgeServerName (SSL * s, int * ad, void * arg) {
111+ return SSL_TLSEXT_ERR_OK ;
112+ }
113+
108114// Reads a 16-bit length prefixed vector from the front of data. Returns
109115// nothing if the length does not fit within data.
110116std::optional<std::span<const uint8_t >> ReadVector16 (
@@ -158,10 +164,7 @@ ssl_select_cert_result_t EarlyClientHelloCallback(const SSL_CLIENT_HELLO* ch) {
158164 // If we can't parse the hello SNI, reject it explicitly:
159165 if (!servername.has_value ()) return ssl_select_cert_error;
160166
161- // Session resumption must only be allowed for sessions with exactly the same
162- // SNI. We use the SNI to derive the session id, so no handshake can resume
163- // from a mismatched SNI value. Mismatches fall back to full handshakes:
164- if (!w->BindSessionsToServerName (*servername)) return ssl_select_cert_error;
167+ if (!w->PinServerName (*servername)) return ssl_select_cert_error;
165168
166169 return ssl_select_cert_success;
167170}
@@ -183,10 +186,10 @@ int EarlyClientHelloCallback(SSL* s, int* al, void* arg) {
183186 return SSL_CLIENT_HELLO_ERROR ;
184187 }
185188
186- // Session resumption must only be allowed for sessions with exactly the same
187- // SNI. We use the SNI to derive the session id, so no handshake can resume
188- // from a mismatched SNI value. Mismatches fall back to full handshakes:
189- if (!w-> BindSessionsToServerName (*servername)) return SSL_CLIENT_HELLO_ERROR ;
189+ if (!w-> PinServerName (*servername)) {
190+ *al = SSL_AD_ILLEGAL_PARAMETER ;
191+ return SSL_CLIENT_HELLO_ERROR ;
192+ }
190193
191194 return SSL_CLIENT_HELLO_SUCCESS ;
192195}
@@ -604,8 +607,7 @@ void TLSWrap::InitSSL() {
604607 // https://github.com/openssl/openssl/issues/7199#issuecomment-420670544
605608 SSL_set_info_callback (ssl_.get (), SSLInfoCallback);
606609
607- if (is_server ())
608- sc_->SetSelectSNIContextCallback (SelectSNIContextCallback);
610+ if (is_server ()) sc_->SetSelectSNIContextCallback (AcknowledgeServerName);
609611
610612 ConfigureSecureContext (sc_.get ());
611613
@@ -1486,39 +1488,34 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) {
14861488 SSL_set_tlsext_host_name (wrap->ssl_ .get (), *servername);
14871489}
14881490
1489- int TLSWrap::SelectSNIContextCallback (SSL * s, int * ad, void * arg) {
1490- TLSWrap* p = static_cast <TLSWrap*>(SSL_get_app_data (s));
1491- Environment* env = p->env ();
1492- HandleScope handle_scope (env->isolate ());
1493- Context::Scope context_scope (env->context ());
1494-
1495- auto servername = SSLPointer::GetServerName (s);
1496- if (!servername.has_value () ||
1497- !Set (env, p->GetOwner (), env->servername_string (), servername.value ()))
1498- return SSL_TLSEXT_ERR_NOACK ;
1499-
1500- Local<Value> ctx = p->object ()->Get (env->context (), env->sni_context_string ())
1501- .FromMaybe (Local<Value>());
1502-
1503- // Acknowlege the name so it's correctly stored on the session:
1504- if (ctx.IsEmpty () || !ctx->IsObject ()) return SSL_TLSEXT_ERR_OK ;
1505-
1506- if (!env->secure_context_constructor_template ()->HasInstance (ctx)) {
1507- // Failure: incorrect SNI context object
1508- Local<Value> err = Exception::TypeError (env->sni_context_err_string ());
1509- p->MakeCallback (env->onerror_string (), 1 , &err);
1510- return SSL_TLSEXT_ERR_NOACK ;
1491+ // The connection's server name is fixed by its first ClientHello: later ones (a
1492+ // renegotiation, or the second of a HelloRetryRequest) must ask for exactly the
1493+ // same, as the connection's secure context was chosen for it.
1494+ bool TLSWrap::PinServerName (std::string_view servername) {
1495+ if (servername_pinned_) {
1496+ if (servername == servername_) return true ;
1497+ Debug (this ,
1498+ " ClientHello asked for server name '%s', connection pinned to '%s'" ,
1499+ servername,
1500+ servername_);
1501+ return false ;
15111502 }
1503+ servername_pinned_ = true ;
1504+ servername_ = servername;
15121505
1513- SecureContext* sc = Unwrap<SecureContext>(ctx. As <Object> ());
1514- CHECK_NOT_NULL (sc );
1515- p-> sni_context_ = BaseObjectPtr<SecureContext>(sc );
1516-
1517- ConfigureSecureContext (sc) ;
1518- CHECK_EQ ( SSL_set_SSL_CTX (p-> ssl_ . get (), sc-> ctx (). get ()), sc-> ctx (). get ());
1519- p-> SetCACerts (sc);
1506+ if (!servername. empty ()) {
1507+ HandleScope handle_scope ( env ()-> isolate () );
1508+ Context::Scope context_scope ( env ()-> context () );
1509+ if (! Set ( env (), GetOwner (), env ()-> servername_string (), servername_)) {
1510+ return false ;
1511+ }
1512+ }
15201513
1521- return SSL_TLSEXT_ERR_OK ;
1514+ // Session resumption must only be allowed for sessions with exactly the same
1515+ // SNI. We use the SNI to derive the session id context, so no handshake can
1516+ // resume from a mismatched SNI value. Mismatches fall back to full
1517+ // handshakes:
1518+ return BindSessionsToServerName (servername);
15221519}
15231520
15241521bool TLSWrap::BindSessionsToServerName (std::string_view servername) {
@@ -1699,6 +1696,7 @@ void TLSWrap::MemoryInfo(MemoryTracker* tracker) const {
16991696 tracker->TrackField (" ocsp_response" , ocsp_response_);
17001697 tracker->TrackField (" sni_context" , sni_context_);
17011698 tracker->TrackField (" error" , error_);
1699+ tracker->TrackField (" servername" , servername_);
17021700 if (pending_cleartext_input_)
17031701 tracker->TrackField (" pending_cleartext_input" , pending_cleartext_input_);
17041702 if (enc_in_ != nullptr )
0 commit comments