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

panda.net.ssl.TrustManagers 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.security.GeneralSecurityException;
import java.security.KeyStore;

import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

/**
 * TrustManager utilities for generating TrustManagers.
 */
public final class TrustManagers {
	/**
	 * Generate a TrustManager that performs no checks.
	 * 
	 * @return the TrustManager
	 */
	public static X509TrustManager acceptAllTrustManager() {
		return TrustedX509TrustManager.INSTANCE;
	}

	/**
	 * Generate a TrustManager that checks server certificates for validity, but otherwise performs
	 * no checks.
	 * 
	 * @return the validating TrustManager
	 */
	public static X509TrustManager validateServerCertificateTrustManager() {
		return CheckServerX509TrustManager.INSTANCE;
	}

	/**
	 * Return the default TrustManager provided by the JVM.
	 * 

* This should be the same as the default used by * {@link javax.net.ssl.SSLContext#init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) * SSLContext#init(KeyManager[], TrustManager[], SecureRandom)} when the TrustManager parameter * is set to {@code null} * * @return the default TrustManager * @throws GeneralSecurityException if an error occurs */ public static X509TrustManager getDefaultTrustManager() throws GeneralSecurityException { return getDefaultTrustManager(null); } /** * Return the default TrustManager provided by the JVM. *

* This should be the same as the default used by * {@link javax.net.ssl.SSLContext#init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) * SSLContext#init(KeyManager[], TrustManager[], SecureRandom)} when the TrustManager parameter * is set to {@code null} * * @param keyStore the KeyStore to use, may be {@code null} * @return the default TrustManager * @throws GeneralSecurityException if an error occurs */ public static X509TrustManager getDefaultTrustManager(KeyStore keyStore) throws GeneralSecurityException { String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory instance = TrustManagerFactory.getInstance(defaultAlgorithm); instance.init(keyStore); return (X509TrustManager)instance.getTrustManagers()[0]; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy