Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.paypal.base;
  
 import java.util.Map;
 
 
Class SSLUtil
 
 public abstract class SSLUtil {

 
 	private static final KeyManagerFactory KMF;

Private java.util.Map used for caching java.security.KeyStores
 
 	private static final Map<StringKeyStoreSTOREMAP;
 
 	static {
 		try {
 			
 			// Initialize KeyManagerFactory and local KeyStore cache
 			 = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
 			 = new HashMap<StringKeyStore>();
 		} catch (NoSuchAlgorithmException e) {
 		}
 	}

Returns a SSLContext

Parameters:
keymanagers KeyManager[] The key managers
Returns:
SSLContext with proper client certificate
Throws:
com.paypal.base.exception.SSLConfigurationException
 
 	public static SSLContext getSSLContext(KeyManager[] keymanagers)
 		try {
 			SSLContext ctx = SSLContext.getInstance("TLS"); // TLS, SSLv3, SSL
 			ctx.init(keymanagersnullnull);
 			return ctx;
 		} catch (Exception e) {
 			throw new SSLConfigurationException(e.getMessage(), e);
 		}
 	}

Retrieves keyStore from the cached java.util.Map, if not present loads certificate into java keyStore and caches it for further references

Parameters:
p12Path Path to the client certificate
password java.security.KeyStore password
Returns:
keyStore java.security.KeyStore loaded with the certificate
Throws:
java.security.NoSuchProviderException
java.security.KeyStoreException
java.security.cert.CertificateException
java.security.NoSuchAlgorithmException
FileNotFoundException
java.io.IOException
 
 	private static KeyStore p12ToKeyStore(String p12PathString password)
 		KeyStore keyStore = .get(p12Path);
 		if (keyStore == null) {
 			keyStore = KeyStore.getInstance("PKCS12""SunJSSE");
 			FileInputStream in = null;
 			try {
 				in = new FileInputStream(p12Path);
 				keyStore.load(inpassword.toCharArray());
 				.put(p12PathkeyStore);
 			} finally {
 				if (in != null) {
 					in.close();
 				}
 			}
 		}
 		return keyStore;
	}

Create a SSLContext with provided client certificate

Parameters:
certPath
certPassword
Returns:
SSLContext
Throws:
com.paypal.base.exception.SSLConfigurationException
	public static SSLContext setupClientSSL(String certPathString certPassword)
		SSLContext sslContext = null;
		try {
			KeyStore ks = p12ToKeyStore(certPathcertPassword);
			.init(kscertPassword.toCharArray());
			sslContext = getSSLContext(.getKeyManagers());
catch (NoSuchAlgorithmException e) {
catch (KeyStoreException e) {
catch (UnrecoverableKeyException e) {
catch (CertificateException e) {
catch (NoSuchProviderException e) {
catch (IOException e) {
		}
		return sslContext;
	}
New to GrepCode? Check out our FAQ X