moresandeep commented on code in PR #1459:
URL: https://github.com/apache/knox/pull/1459#discussion_r4324223474


##########
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 {

Review Comment:
   Agreed! I'll update it.



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