[ 
https://issues.apache.org/jira/browse/KNOX-3519?focusedWorklogId=1048455&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1048455
 ]

ASF GitHub Bot logged work on KNOX-3519:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 20/Oct/26 11:40
            Start Date: 20/Oct/26 11:40
    Worklog Time Spent: 10m 
      Work Description: moresandeep commented on code in PR #1459:
URL: https://github.com/apache/knox/pull/1459#discussion_r4324512319


##########
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/SetHostSSLSocketFactory.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ */
+package org.apache.knox.gateway.services.token.impl;
+
+import javax.net.ssl.SSLSocketFactory;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * An {@link SSLSocketFactory} decorator that stamps a fixed host onto every 
socket it creates.
+ * <p>
+ * BouncyCastle's JSSE takes the hostname for HTTPS endpoint identification 
from the socket itself
+ * rather than from the URL. {@link java.net.HttpURLConnection}, which Nimbus 
uses to fetch a JWKS,
+ * creates the socket without a host and connects it by address, so under the 
BouncyCastle FIPS
+ * provider the hostname arrives as {@code null} and endpoint identification 
fails with
+ * "No hostname specified for HTTPS endpoint ID check" before the certificate 
is ever examined.
+ * That happens even when the certificate carries a matching iPAddress SAN.
+ * <p>
+ * Setting the host restores the check: BouncyCastle matches an IP literal 
against the
+ * certificate's iPAddress SANs and a name against its dNSName SANs. 
Verification is preserved,
+ * not bypassed.
+ * <p>
+ * The host is applied through {@code setHost(String)}, declared by
+ * {@code org.bouncycastle.jsse.BCSSLSocket}. That type is reached 
reflectively so Knox does not
+ * take a compile dependency on the BouncyCastle TLS provider, which is 
present only in FIPS
+ * deployments. On sockets that do not expose the method this decorator is a 
no-op, which is
+ * correct for the stock JSSE: it identifies IP endpoints from the URL already.
+ */
+class SetHostSSLSocketFactory extends SSLSocketFactory {
+
+  private static final String SET_HOST = "setHost";
+
+  private final SSLSocketFactory delegate;
+  private final String host;
+
+  SetHostSSLSocketFactory(SSLSocketFactory delegate, String host) {
+    this.delegate = delegate;
+    this.host = host;
+  }
+
+  /**
+   * Wraps {@code base} so sockets created for {@code url} carry that URL's 
host.
+   *
+   * @return the wrapped factory, or {@code base} unchanged when the URL has 
no usable host
+   */
+  static SSLSocketFactory forUrl(URL url, SSLSocketFactory base) {
+    final String host = url == null ? null : url.getHost();
+    if (host == null || host.isEmpty()) {
+      return base;
+    }
+    return new SetHostSSLSocketFactory(base, host);
+  }
+
+  /**
+   * Applies {@code host} to {@code socket} via a public {@code 
setHost(String)} method.
+   *
+   * @return {@code true} when the host was applied, {@code false} when the 
socket does not
+   *         support it or the call failed
+   */
+  static boolean setHost(Socket socket, String host) {
+    if (socket == null || host == null || host.isEmpty()) {
+      return false;
+    }
+    for (Class<?> type : declaringTypes(socket.getClass())) {
+      final Method method;
+      try {
+        method = type.getMethod(SET_HOST, String.class);
+      } catch (NoSuchMethodException e) {
+        continue;
+      }
+      try {
+        method.invoke(socket, host);
+        return true;
+      } catch (IllegalAccessException e) {
+        /* Not reachable through this type; a public interface may still 
expose it. */
+        continue;
+      } catch (InvocationTargetException | RuntimeException e) {
+        return false;

Review Comment:
   The `IllegalAccessException` which we tolerate and keep moving is caused due 
to reflection access, with this exception we know we hit, say a private method 
in impl e.g. `org.bouncycastle.jsse.provider.ProvSSLSocketDirect` 
   
   `InvocationTargetException` and `RuntimeException` errors will be thown in 
(or within) the method body, e.g. `BCSSLSocket.setHost` (interface) and 
`ProvSSLSocketDirect.setHost` (concrete impl) land on the same method body so 
If the call itself throws, continuing the walk will invokes the identical code 
and gets an identical exception. So, we'll just be throwing errors and not 
gaining much. 
    
   I will make sure to add docs to the method, let me know if this explanation 
helps!





Issue Time Tracking
-------------------

    Worklog Id:     (was: 1048455)
    Time Spent: 1h 10m  (was: 1h)

> Set the JWKS host on the TLS socket so HTTPS endpoint identification works 
> under BC-FIPS
> ----------------------------------------------------------------------------------------
>
>                 Key: KNOX-3519
>                 URL: https://issues.apache.org/jira/browse/KNOX-3519
>             Project: Apache Knox
>          Issue Type: Bug
>            Reporter: Sandeep More
>            Assignee: Sandeep More
>            Priority: Major
>          Time Spent: 1h 10m
>  Remaining Estimate: 0h
>
> BC-FIPS HostnameUtil.checkHostname refuses IP literals for the HTTPS 
> algorithm, it matches DNS names only and never consults iPAddress SANs. So 
> the cert's IP Address:10.83.6.18 SAN is irrelevant, and no truststore change 
> can fix it. 
> THe result is when an ip address is used for SSL handshake throwing this error
> {code:java}
> 2026-10-19 23:15:18,262 42f8a538-720e-4467-ac21-e71c95f6f9c3 ERROR 
> token.state (DefaultTokenAuthorityService.java:verifyToken(371)) - Failed to 
> verify token using JWKS endpoint https://10.83.6.18:6443/openid/v1/jwks, 
> reason: com.nimbusds.jose.jwk.source.JWKSetRetrievalException: Couldn't 
> retrieve JWK set from URL: certificate_unknown(46)
> org.apache.knox.gateway.services.security.token.TokenServiceException: Cannot 
> verify token.
>         at 
> org.apache.knox.gateway.services.token.impl.DefaultTokenAuthorityService.verifyToken(DefaultTokenAuthorityService.java:331)
>         at 
> org.apache.knox.gateway.services.token.impl.DefaultTokenAuthorityService.verifyToken(DefaultTokenAuthorityService.java:365)
>         at 
> org.apache.knox.gateway.provider.federation.jwt.filter.AbstractJWTFilter.verifyTokenSignature(AbstractJWTFilter.java:966)
>         at 
> org.apache.knox.gateway.provider.federation.jwt.filter.AbstractJWTFilter.doFullTokenValidation(AbstractJWTFilter.java:765)
>         at 
> org.apache.knox.gateway.provider.federation.jwt.filter.AbstractJWTFilter.validateToken(AbstractJWTFilter.java:683)
>         at 
> org.apache.knox.gateway.provider.federation.jwt.filter.JWTFederationFilter.parseAndValidateJWT(JWTFederationFilter.java:733)
>         at 
> org.apache.knox.gateway.provider.federation.jwt.filter.JWTFederationFilter.parseAndValidateJWT(JWTFederationFilter.java:720)
>         at 
> org.apache.knox.gateway.provider.federation.jwt.filter.JWTFederationFilter.doFilter(JWTFederationFilter.java:346)
>         at 
> org.apache.knox.gateway.GatewayFilter$Holder.doFilter(GatewayFilter.java:391)
>         at 
> org.apache.knox.gateway.GatewayFilter$Chain.doFilter(GatewayFilter.java:305)
>         at 
> org.apache.knox.gateway.filter.XForwardedHeaderFilter.doFilter(XForwardedHeaderFilter.java:50)
>         at 
> org.apache.knox.gateway.filter.AbstractGatewayFilter.doFilter(AbstractGatewayFilter.java:59)
>         at 
> org.apache.knox.gateway.GatewayFilter$Holder.doFilter(GatewayFilter.java:391)
>         at 
> org.apache.knox.gateway.GatewayFilter$Chain.doFilter(GatewayFilter.java:305)
>         at 
> org.apache.knox.gateway.GatewayFilter.doFilter(GatewayFilter.java:191)
>         at 
> org.apache.knox.gateway.GatewayFilter.doFilter(GatewayFilter.java:102)
>         at 
> org.apache.knox.gateway.GatewayServlet.service(GatewayServlet.java:127)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletHolder$NotAsync.service(ServletHolder.java:1387)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletHolder.handle(ServletHolder.java:753)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletHandler$ChainEnd.doFilter(ServletHandler.java:1624)
>         at 
> org.eclipse.jetty.ee10.websocket.servlet.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:195)
>         at 
> org.eclipse.jetty.ee10.servlet.FilterHolder.doFilter(FilterHolder.java:205)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletHandler$Chain.doFilter(ServletHandler.java:1596)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletHandler$MappedServlet.handle(ServletHandler.java:1557)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletChannel.dispatch(ServletChannel.java:828)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletChannel.handle(ServletChannel.java:438)
>         at 
> org.eclipse.jetty.ee10.servlet.ServletHandler.handle(ServletHandler.java:470)
>         at 
> org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:575)
>         at 
> org.eclipse.jetty.ee10.servlet.SessionHandler.handle(SessionHandler.java:717)
>         at 
> org.eclipse.jetty.server.handler.ContextHandler.handle(ContextHandler.java:1102)
>         at 
> org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:181)
>         at org.eclipse.jetty.server.Handler$Wrapper.handle(Handler.java:740)
>         at 
> org.apache.knox.gateway.trace.TraceHandler.handle(TraceHandler.java:72)
>         at org.eclipse.jetty.server.Handler$Wrapper.handle(Handler.java:740)
>         at 
> org.apache.knox.gateway.filter.CorrelationHandler.handle(CorrelationHandler.java:50)
>         at org.eclipse.jetty.server.Handler$Wrapper.handle(Handler.java:740)
>         at 
> org.apache.knox.gateway.filter.PortMappingHelperHandler.handle(PortMappingHelperHandler.java:104)
>         at org.eclipse.jetty.server.Server.handle(Server.java:182)
>         at 
> org.eclipse.jetty.server.internal.HttpChannelState$HandlerInvoker.run(HttpChannelState.java:772)
>         at 
> org.eclipse.jetty.server.internal.HttpConnection.onFillable(HttpConnection.java:415)
>         at 
> org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:323)
>         at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105)
>         at 
> org.eclipse.jetty.io.SelectableChannelEndPoint$1.run(SelectableChannelEndPoint.java:53)
>         at 
> org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.runTask(AdaptiveExecutionStrategy.java:492)
>         at 
> org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.epcRunTask(AdaptiveExecutionStrategy.java:428)
>         at 
> org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.consumeTask(AdaptiveExecutionStrategy.java:401)
>         at 
> org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.tryProduce(AdaptiveExecutionStrategy.java:255)
>         at 
> org.eclipse.jetty.util.thread.strategy.AdaptiveExecutionStrategy.run(AdaptiveExecutionStrategy.java:204)
>         at 
> org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:311)
>         at 
> org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:981)
>         at 
> org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.doRunJob(QueuedThreadPool.java:1212)
>         at 
> org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1166)
>         at java.base/java.lang.Thread.run(Thread.java:840)
> Caused by: com.nimbusds.jose.jwk.source.JWKSetRetrievalException: Couldn't 
> retrieve JWK set from URL: certificate_unknown(46)
>         at 
> com.nimbusds.jose.jwk.source.URLBasedJWKSetSource.getJWKSet(URLBasedJWKSetSource.java:92)
>         at 
> com.nimbusds.jose.jwk.source.RetryingJWKSetSource.getJWKSet(RetryingJWKSetSource.java:99)
>         at 
> com.nimbusds.jose.jwk.source.OutageTolerantJWKSetSource.getJWKSet(OutageTolerantJWKSetSource.java:113)
>         at 
> com.nimbusds.jose.jwk.source.RateLimitedJWKSetSource.getJWKSet(RateLimitedJWKSetSource.java:107)
>         at 
> com.nimbusds.jose.jwk.source.CachingJWKSetSource.loadJWKSetNotThreadSafe(CachingJWKSetSource.java:330)
>         at 
> com.nimbusds.jose.jwk.source.RefreshAheadCachingJWKSetSource.loadJWKSetNotThreadSafe(RefreshAheadCachingJWKSetSource.java:347)
>         at 
> com.nimbusds.jose.jwk.source.CachingJWKSetSource.loadJWKSetBlocking(CachingJWKSetSource.java:240)
>         at 
> com.nimbusds.jose.jwk.source.RefreshAheadCachingJWKSetSource.getJWKSet(RefreshAheadCachingJWKSetSource.java:326)
>         at 
> com.nimbusds.jose.jwk.source.JWKSetBasedJWKSource.get(JWKSetBasedJWKSource.java:76)
>         at 
> com.nimbusds.jose.proc.JWSVerificationKeySelector.selectJWSKeys(JWSVerificationKeySelector.java:150)
>         at 
> com.nimbusds.jwt.proc.DefaultJWTProcessor.selectKeys(DefaultJWTProcessor.java:320)
>         at 
> com.nimbusds.jwt.proc.DefaultJWTProcessor.process(DefaultJWTProcessor.java:391)
>         at 
> com.nimbusds.jwt.proc.DefaultJWTProcessor.process(DefaultJWTProcessor.java:340)
>         at 
> com.nimbusds.jwt.proc.DefaultJWTProcessor.process(DefaultJWTProcessor.java:331)
>         at 
> org.apache.knox.gateway.services.token.impl.DefaultTokenAuthorityService.verifyToken(DefaultTokenAuthorityService.java:319)
>         ... 52 more
> Caused by: org.bouncycastle.tls.TlsFatalAlert: certificate_unknown(46)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvSSLSocketDirect.checkServerTrusted(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvTlsClient$1.notifyServerCertificate(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsUtils.processServerCertificate(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsClientProtocol.handleServerCertificate(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsClientProtocol.receive13ServerCertificate(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsClientProtocol.handle13HandshakeMessage(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsClientProtocol.handleHandshakeMessage(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvTlsClientProtocol.handleHandshakeMessage(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsProtocol.processHandshakeQueue(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsProtocol.processRecord(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.RecordStream.readRecord(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsProtocol.safeReadRecord(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsProtocol.blockForHandshake(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.tls.TlsClientProtocol.connect(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvSSLSocketDirect.startHandshake(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvSSLSocketDirect.startHandshake(Unknown
>  Source)
>         at 
> java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:596)
>         at 
> java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:190)
>         at 
> java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1719)
>         at 
> java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1644)
>         at 
> java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
>         at 
> com.nimbusds.jose.util.DefaultResourceRetriever.getInputStream(DefaultResourceRetriever.java:366)
>         at 
> com.nimbusds.jose.util.DefaultResourceRetriever.retrieveResource(DefaultResourceRetriever.java:269)
>         at 
> com.nimbusds.jose.jwk.source.URLBasedJWKSetSource.getJWKSet(URLBasedJWKSetSource.java:90)
>         ... 66 more
> Caused by: java.security.cert.CertificateException: No hostname specified for 
> HTTPS endpoint ID check
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.HostnameUtil.checkHostname(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvX509TrustManager.checkEndpointID(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvX509TrustManager.checkEndpointID(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvX509TrustManager.checkExtendedTrust(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvX509TrustManager.checkTrusted(Unknown
>  Source)
>         at 
> org.bouncycastle.fips.tls/org.bouncycastle.jsse.provider.ProvX509TrustManager.checkServerTrusted(Unknown
>  Source)
>         ... 90 more {code}
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to