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! -- 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]
