KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > SSLKeyManager


1 package com.sslexplorer.boot;
2
3 import java.io.IOException JavaDoc;
4 import java.security.KeyStore JavaDoc;
5 import java.security.KeyStoreException JavaDoc;
6 import java.security.NoSuchAlgorithmException JavaDoc;
7 import java.security.UnrecoverableKeyException JavaDoc;
8 import java.security.cert.CertificateException JavaDoc;
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 JavaDoc keyStore = km.getKeyStore();
27                 // Get a key manager factory out of the key store
28
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 JavaDoc e) {
34             log.fatal("Error getting algorithm.", e);
35         } catch (CertificateException JavaDoc e) {
36             log.fatal("Error loading certificate.", e);
37         } catch (IOException JavaDoc e) {
38             log.fatal("I/O issue.", e);
39         } catch (KeyStoreException JavaDoc e1) {
40             log.fatal("Error loading keystore instance.", e1);
41         } catch (UnrecoverableKeyException JavaDoc e) {
42             log.fatal("Can't recover the key.", e);
43         } catch (Exception JavaDoc e) {
44             log.fatal("Unknown error", e);
45         }
46         return retVal;
47     }
48 }
49
Popular Tags