1 17 package org.apache.servicemix.jbi.security.keystore.impl; 18 19 import java.io.BufferedInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.security.Key ; 25 import java.security.KeyStore ; 26 import java.security.KeyStoreException ; 27 import java.security.NoSuchAlgorithmException ; 28 import java.security.PrivateKey ; 29 import java.security.UnrecoverableKeyException ; 30 import java.security.cert.Certificate ; 31 import java.security.cert.CertificateException ; 32 import java.util.ArrayList ; 33 import java.util.Enumeration ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 import javax.net.ssl.KeyManager; 39 import javax.net.ssl.KeyManagerFactory; 40 import javax.net.ssl.TrustManager; 41 import javax.net.ssl.TrustManagerFactory; 42 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.apache.servicemix.jbi.security.keystore.KeystoreInstance; 46 import org.apache.servicemix.jbi.security.keystore.KeystoreIsLocked; 47 import org.springframework.core.io.Resource; 48 49 53 public class FileKeystoreInstance implements KeystoreInstance { 54 55 private static final Log log = LogFactory.getLog(FileKeystoreInstance.class); 56 private final static String JKS = "JKS"; 57 58 private Resource path; 59 private String name; 60 private String keystorePassword; 61 private Map keyPasswords = new HashMap (); 62 private File keystoreFile; 64 private List privateKeys = new ArrayList (); 66 private List trustCerts = new ArrayList (); 67 private KeyStore keystore; 68 private long keystoreReadDate = Long.MIN_VALUE; 69 70 73 public void setKeyPasswords(String keyPasswords) { 74 if (keyPasswords != null) { 75 String [] keys = keyPasswords.split("\\]\\!\\["); 76 for (int i = 0; i < keys.length; i++) { 77 String key = keys[i]; 78 int pos = key.indexOf('='); 79 this.keyPasswords.put(key.substring(0, pos), key.substring(pos+1).toCharArray()); 80 } 81 } 82 } 83 84 87 public String getName() { 88 return name; 89 } 90 91 94 public void setName(String keystoreName) { 95 this.name = keystoreName; 96 } 97 98 101 public void setKeystorePassword(String keystorePassword) { 102 this.keystorePassword = keystorePassword; 103 } 104 105 108 public Resource getPath() { 109 return path; 110 } 111 112 115 public void setPath(Resource keystorePath) throws IOException { 116 this.path = keystorePath; 117 this.keystoreFile = keystorePath.getFile(); 118 } 119 120 public Certificate getCertificate(String alias) { 121 if (!loadKeystoreData()) { 122 return null; 123 } 124 try { 125 return keystore.getCertificate(alias); 126 } catch (KeyStoreException e) { 127 log.error("Unable to read certificate from keystore", e); 128 } 129 return null; 130 } 131 132 public String getCertificateAlias(Certificate cert) { 133 if (!loadKeystoreData()) { 134 return null; 135 } 136 try { 137 return keystore.getCertificateAlias(cert); 138 } catch (KeyStoreException e) { 139 log.error("Unable to read retrieve alias for given certificate from keystore", e); 140 } 141 return null; 142 } 143 144 public Certificate [] getCertificateChain(String alias) { 145 if (!loadKeystoreData()) { 146 return null; 147 } 148 try { 149 return keystore.getCertificateChain(alias); 150 } catch (KeyStoreException e) { 151 log.error("Unable to read certificate chain from keystore", e); 152 } 153 return null; 154 } 155 156 public KeyManager[] getKeyManager(String algorithm, String keyAlias) throws KeystoreIsLocked, NoSuchAlgorithmException , KeyStoreException , UnrecoverableKeyException { 157 if(isKeystoreLocked()) { 158 throw new KeystoreIsLocked("Keystore '"+name+"' is locked; please unlock it in the console."); 159 } 160 if(keystore == null || keystoreReadDate < keystoreFile.lastModified()) { 161 loadKeystoreData(); 162 } 163 KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(algorithm); 164 keyFactory.init(keystore, (char[]) keyPasswords.get(keyAlias)); 165 return keyFactory.getKeyManagers(); 166 } 167 168 public PrivateKey getPrivateKey(String alias) { 169 if (!loadKeystoreData()) { 170 return null; 171 } 172 try { 173 if (isKeyLocked(alias)) { 174 return null; 175 } 176 Key key = keystore.getKey(alias, (char[]) keyPasswords.get(alias)); 177 if (key instanceof PrivateKey ) { 178 return (PrivateKey ) key; 179 } 180 } catch (KeyStoreException e) { 181 log.error("Unable to read private key from keystore", e); 182 } catch (NoSuchAlgorithmException e) { 183 log.error("Unable to read private key from keystore", e); 184 } catch (UnrecoverableKeyException e) { 185 log.error("Unable to read private key from keystore", e); 186 } 187 return null; 188 } 189 190 public TrustManager[] getTrustManager(String algorithm) throws KeyStoreException , NoSuchAlgorithmException , KeystoreIsLocked { 191 if(isKeystoreLocked()) { 192 throw new KeystoreIsLocked("Keystore '"+name+"' is locked; please unlock it in the console."); 193 } 194 if (!loadKeystoreData()) { 195 return null; 196 } 197 TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(algorithm); 198 trustFactory.init(keystore); 199 return trustFactory.getTrustManagers(); 200 } 201 202 public boolean isKeyLocked(String keyAlias) { 203 return keyPasswords.get(keyAlias) == null; 204 } 205 206 public boolean isKeystoreLocked() { 207 return keystorePassword == null; 208 } 209 210 public String [] listPrivateKeys() { 211 if (!loadKeystoreData()) { 212 return null; 213 } 214 return (String []) privateKeys.toArray(new String [privateKeys.size()]); 215 } 216 217 public String [] listTrustCertificates() { 218 if (!loadKeystoreData()) { 219 return null; 220 } 221 return (String []) trustCerts.toArray(new String [trustCerts.size()]); 222 } 223 224 226 private boolean loadKeystoreData() { 227 if (keystoreFile == null) { 228 throw new IllegalArgumentException ("keystorePath not set"); 229 } 230 if (keystoreReadDate >= keystoreFile.lastModified()) { 231 return true; 232 } 233 if (!keystoreFile.exists() || !keystoreFile.canRead()) { 234 throw new IllegalArgumentException ("Invalid keystore file (" + path + " = " + keystoreFile.getAbsolutePath() + ")"); 235 } 236 try { 237 keystoreReadDate = System.currentTimeMillis(); 238 privateKeys.clear(); 239 trustCerts.clear(); 240 if(keystore == null) { 241 keystore = KeyStore.getInstance(JKS); 242 } 243 InputStream in = new BufferedInputStream (new FileInputStream (keystoreFile)); 244 keystore.load(in, keystorePassword == null ? new char[0] : keystorePassword.toCharArray()); 245 in.close(); 246 Enumeration aliases = keystore.aliases(); 247 while (aliases.hasMoreElements()) { 248 String alias = (String ) aliases.nextElement(); 249 if (keystore.isKeyEntry(alias)) { 250 privateKeys.add(alias); 251 } else if (keystore.isCertificateEntry(alias)) { 252 trustCerts.add(alias); 253 } 254 } 255 return true; 256 } catch (KeyStoreException e) { 257 log.error("Unable to open keystore with provided password", e); 258 } catch (IOException e) { 259 log.error("Unable to open keystore with provided password", e); 260 } catch (NoSuchAlgorithmException e) { 261 log.error("Unable to open keystore with provided password", e); 262 } catch (CertificateException e) { 263 log.error("Unable to open keystore with provided password", e); 264 } 265 return false; 266 } 267 268 } 269 | Popular Tags |