All Downloads are FREE. Search and download functionalities are using the official Maven repository.

panda.net.ssl.SSLSockets Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.net.ssl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.net.ssl.SSLSocket;

/**
 * General utilities for SSLSocket.
 */
public abstract class SSLSockets {
	/**
	 * Enable the HTTPS endpoint identification algorithm on an SSLSocket.
	 * 
	 * @param socket the SSL socket
	 * @return {@code true} on success (this is only supported on Java 1.7+)
	 */
	public static boolean enableEndpointNameVerification(SSLSocket socket) {
		try {
			Class cls = Class.forName("javax.net.ssl.SSLParameters");
			Method setEndpointIdentificationAlgorithm = cls.getDeclaredMethod("setEndpointIdentificationAlgorithm",
				String.class);
			Method getSSLParameters = SSLSocket.class.getDeclaredMethod("getSSLParameters");
			Method setSSLParameters = SSLSocket.class.getDeclaredMethod("setSSLParameters", cls);
			if (setEndpointIdentificationAlgorithm != null && getSSLParameters != null && setSSLParameters != null) {
				Object sslParams = getSSLParameters.invoke(socket);
				if (sslParams != null) {
					setEndpointIdentificationAlgorithm.invoke(sslParams, "HTTPS");
					setSSLParameters.invoke(socket, sslParams);
					return true;
				}
			}
		}
		catch (SecurityException e) { // Ignored
		}
		catch (ClassNotFoundException e) { // Ignored
		}
		catch (NoSuchMethodException e) { // Ignored
		}
		catch (IllegalArgumentException e) { // Ignored
		}
		catch (IllegalAccessException e) { // Ignored
		}
		catch (InvocationTargetException e) { // Ignored
		}
		return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy