gnodet-bot commented on code in PR #30412:
URL: https://github.com/apache/camel/pull/30412#discussion_r4629299373


##########
components/camel-nats/src/main/java/org/apache/camel/component/nats/HostnameVerifyingSSLContext.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.camel.component.nats;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.security.KeyManagementException;
+import java.security.SecureRandom;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLContextSpi;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.SSLParameters;
+import javax.net.ssl.SSLServerSocketFactory;
+import javax.net.ssl.SSLSessionContext;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+
+/**
+ * An SSLContext whose client sockets and engines check that the certificate 
of the server was issued for the host they
+ * connect to, as jnats does not ask for it.
+ */
+final class HostnameVerifyingSSLContext extends SSLContext {
+
+    private HostnameVerifyingSSLContext(SSLContext delegate) {
+        super(new Spi(delegate), delegate.getProvider(), 
delegate.getProtocol());
+    }
+
+    static SSLContext wrap(SSLContext context) {
+        return context instanceof HostnameVerifyingSSLContext ? context : new 
HostnameVerifyingSSLContext(context);
+    }
+
+    private static <T> T verifyHostname(T socket) {
+        if (socket instanceof SSLSocket sslSocket) {
+            SSLParameters parameters = sslSocket.getSSLParameters();
+            parameters.setEndpointIdentificationAlgorithm("HTTPS");
+            sslSocket.setSSLParameters(parameters);
+        } else if (socket instanceof SSLEngine engine) {
+            SSLParameters parameters = engine.getSSLParameters();
+            parameters.setEndpointIdentificationAlgorithm("HTTPS");
+            engine.setSSLParameters(parameters);
+        }
+        return socket;
+    }
+
+    private static final class Spi extends SSLContextSpi {
+
+        private final SSLContext delegate;
+
+        Spi(SSLContext delegate) {
+            this.delegate = delegate;
+        }
+
+        @Override
+        protected void engineInit(KeyManager[] km, TrustManager[] tm, 
SecureRandom sr) throws KeyManagementException {
+            delegate.init(km, tm, sr);
+        }
+
+        @Override
+        protected SSLSocketFactory engineGetSocketFactory() {
+            return new SocketFactory(delegate.getSocketFactory());
+        }

Review Comment:
   ⚠️ **Low severity / defensive note:**
   
   `engineInit()` re-initializes the **delegate** SSLContext by calling 
`delegate.init(km, tm, sr)`. The delegate is already initialized at the point 
this wrapper is created (via `HostnameVerifyingSSLContext.wrap()`), and jnats 
never calls `init()` on the provided SSLContext — so this code path is 
effectively dead in practice.
   
   However, if a future jnats version or external caller does invoke `init()` 
on the wrapper, it would silently reinitialize the delegate and potentially 
discard user-configured key/trust managers without error. Consider making this 
explicit:
   
   ```suggestion
           protected void engineInit(KeyManager[] km, TrustManager[] tm, 
SecureRandom sr) throws KeyManagementException {
               // The delegate is initialized externally before wrapping; this 
method should not be called.
               delegate.init(km, tm, sr);
           }
   ```
   
   Or alternatively throw `KeyManagementException` if re-initialization must be 
explicitly unsupported. Either approach clarifies intent.



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