bneradt commented on code in PR #13949:
URL: https://github.com/apache/trafficserver/pull/13949#discussion_r4470192785


##########
src/iocore/net/SSLUtils.cc:
##########
@@ -256,28 +257,37 @@ ssl_client_hello_callback(const SSL_CLIENT_HELLO 
*client_hello)
   }
 
   TLSSNISupport *snis = TLSSNISupport::getInstance(s);
-  if (snis) {
-    snis->on_client_hello(ch);
-    int ret = snis->perform_sni_action(*s);
-    if (ret != SSL_TLSEXT_ERR_OK) {
-      return CLIENT_HELLO_ERROR;
-    }
-  } else {
+  if (snis == nullptr) {
     // This error suggests either of these:
     // 1) Call back on unsupported netvc -- Don't register callback 
unnecessarily
     // 2) Call back on stale netvc
     Dbg(dbg_ctl_ssl_error, "ssl_client_hello_callback was called 
unexpectedly");
     return CLIENT_HELLO_ERROR;
   }
 
+  snis->on_client_hello(ch);
+  ts::PostScript clear_client_hello([snis]() -> void { 
snis->clear_client_hello(); });
+
+  // When a delayed ClientHello hook reenables, the TLS library calls back 
again for the remaining
+  // hooks, and the SNI actions have already run.
+  if (es == nullptr || !es->resuming_client_hello_hooks()) {
+    int ret = snis->perform_sni_action(*s);
+    if (ret != SSL_TLSEXT_ERR_OK) {
+      return CLIENT_HELLO_ERROR;
+    }
+  }
+
   if (es) {
     if (es->finished_client_hello_hooks()) {
       return CLIENT_HELLO_SUCCESS;
     }
-    bool reenabled = es->callHooks(TS_EVENT_SSL_CLIENT_HELLO);
-    if (!reenabled) {
-      return CLIENT_HELLO_RETRY;
-    }
+    // Hooks that reenable from within their invocation leave the rest to run 
here.
+    do {
+      bool reenabled = es->callHooks(TS_EVENT_SSL_CLIENT_HELLO);
+      if (!reenabled) {
+        return CLIENT_HELLO_RETRY;
+      }
+    } while (es->resuming_client_hello_hooks());

Review Comment:
   Good catch, thanks. I added a bullet to the description that covers the 
OpenSSL bind fix, why BoringSSL was unaffected, and what this means for 
backports. I also noted it in the commit message.



##########
include/iocore/net/TLSSNISupport.h:
##########
@@ -106,6 +106,10 @@ class TLSSNISupport
    */
   void on_client_hello(ClientHello &client_hello);
 
+  /** Forget the CLIENT_HELLO passed to on_client_hello(), which is only valid 
during the ClientHello callback.
+   */
+  void clear_client_hello();

Review Comment:
   Done. The sentence now says `TSVConnClientHelloGet` called from the 
`TS_EVENT_SSL_CLIENT_HELLO` hook.



##########
src/iocore/net/SSLNetVConnection.cc:
##########
@@ -1789,6 +1790,14 @@ SSLNetVConnection::reenable(int event)
     sslHandshakeStatus = SSLHandshakeStatus::SSL_HANDSHAKE_ERROR;
   }
 
+  // The ClientHello is only readable during the TLS library's ClientHello 
callback, which
+  // the resumed handshake calls again to run the remaining ClientHello hooks.
+  if (this->get_handshake_hook_state() == 
TLSEventSupport::SSLHandshakeHookState::HANDSHAKE_HOOKS_CLIENT_HELLO_INVOKE) {

Review Comment:
   I agree that a doc note is enough, and I added one to 
`TS_SSL_CLIENT_HELLO_HOOK` in ssl-hooks.en.rst: switch the context from the 
cert hook instead. One update after rebasing: the RFC 7250 raw public key 
change (#13548) now calls `_set_handshake_callbacks()` on every context from 
`ssl_multicert`, not just `*`. So a switch to a context returned by 
`TSSslContextFindByName()` still re-enters the ClientHello callback. The note 
therefore scopes the risk to contexts Traffic Server did not create, such as a 
plugin's own `SSL_CTX_new()`. That raw public key change also switches the 
context inside the ClientHello callback. In this PR, that selection runs only 
on the first callback, together with the SNI actions.



##########
src/iocore/net/SSLNetVConnection.cc:
##########
@@ -1789,6 +1790,14 @@ SSLNetVConnection::reenable(int event)
     sslHandshakeStatus = SSLHandshakeStatus::SSL_HANDSHAKE_ERROR;
   }
 
+  // The ClientHello is only readable during the TLS library's ClientHello 
callback, which
+  // the resumed handshake calls again to run the remaining ClientHello hooks.
+  if (this->get_handshake_hook_state() == 
TLSEventSupport::SSLHandshakeHookState::HANDSHAKE_HOOKS_CLIENT_HELLO_INVOKE) {
+    this->resume_tls_event();
+    this->readReschedule(nh);
+    return;
+  }
+
   this->resume_tls_event();
 
   // Reenabling from the handshake callback

Review Comment:
   Done. The comment now covers only the other stages.



##########
src/iocore/net/TLSEventSupport.cc:
##########
@@ -451,6 +451,19 @@ TLSEventSupport::finished_client_hello_hooks() const
   }
 }
 
+/** Whether a ClientHello hook has reenabled the connection with ClientHello 
hooks left to run.

Review Comment:
   Done. I used your wording.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to