1 package com.sslexplorer.boot; 2 3 import java.io.IOException ; 4 import java.security.KeyStore ; 5 import java.security.KeyStoreException ; 6 import java.security.NoSuchAlgorithmException ; 7 import java.security.UnrecoverableKeyException ; 8 import java.security.cert.CertificateException ; 9 10 import javax.net.ssl.KeyManager; 11 import javax.net.ssl.KeyManagerFactory; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 16 public class SSLKeyManager { 17 18 private static Log log = LogFactory.getLog(SSLKeyManager.class); 19 20 public static KeyManager[] getKeyManagerArray() { 21 KeyManager[] retVal = null; 22 23 try { 24 KeyStoreManager km = KeyStoreManager.getInstance(KeyStoreManager.SERVER_AUTHENTICATION_CERTIFICATES_KEY_STORE); 25 if (!km.isKeyStoreEmpty()) { 26 KeyStore keyStore = km.getKeyStore(); 27 KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); 29 keyManagerFactory.init(keyStore, KeyStoreManager.getInstance(KeyStoreManager.SERVER_AUTHENTICATION_CERTIFICATES_KEY_STORE).getKeyStorePassword().toCharArray()); 30 retVal = keyManagerFactory.getKeyManagers(); 31 } else 32 return null; 33 } catch (NoSuchAlgorithmException e) { 34 log.fatal("Error getting algorithm.", e); 35 } catch (CertificateException e) { 36 log.fatal("Error loading certificate.", e); 37 } catch (IOException e) { 38 log.fatal("I/O issue.", e); 39 } catch (KeyStoreException e1) { 40 log.fatal("Error loading keystore instance.", e1); 41 } catch (UnrecoverableKeyException e) { 42 log.fatal("Can't recover the key.", e); 43 } catch (Exception e) { 44 log.fatal("Unknown error", e); 45 } 46 return retVal; 47 } 48 } 49 | Popular Tags |