1 17 package org.apache.servicemix.soap.handlers.security; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.security.GeneralSecurityException ; 22 import java.security.Key ; 23 import java.security.KeyStore ; 24 import java.security.KeyStoreException ; 25 import java.security.PrivateKey ; 26 import java.security.cert.Certificate ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.HashSet ; 30 import java.util.List ; 31 import java.util.Set ; 32 33 import org.apache.ws.security.components.crypto.CredentialException; 34 import org.springframework.core.io.Resource; 35 36 public class StandaloneCrypto extends BaseCrypto { 37 38 private Resource keyStoreUrl; 39 private String keyStoreType; 40 private String keyStorePassword; 41 private KeyStore keyStore; 42 private String keyPassword; 43 44 47 public String getKeyPassword() { 48 return keyPassword; 49 } 50 51 54 public void setKeyPassword(String keyPassword) { 55 this.keyPassword = keyPassword; 56 } 57 58 61 public String getKeyStorePassword() { 62 return keyStorePassword; 63 } 64 65 68 public void setKeyStorePassword(String keyStorePassword) { 69 this.keyStorePassword = keyStorePassword; 70 } 71 72 75 public String getKeyStoreType() { 76 return keyStoreType; 77 } 78 79 82 public void setKeyStoreType(String keyStoreType) { 83 this.keyStoreType = keyStoreType; 84 } 85 86 89 public Resource getKeyStoreUrl() { 90 return keyStoreUrl; 91 } 92 93 96 public void setKeyStoreUrl(Resource keyStoreUrl) { 97 this.keyStoreUrl = keyStoreUrl; 98 } 99 100 protected String [] getAliases() throws KeyStoreException { 101 List aliases = Collections.list(loadKeyStore().aliases()); 102 return (String []) aliases.toArray(new String [aliases.size()]); 103 } 104 105 protected Certificate getCertificate(String alias) throws KeyStoreException { 106 return loadKeyStore().getCertificate(alias); 107 } 108 109 protected String getCertificateAlias(Certificate cert) throws KeyStoreException { 110 return loadKeyStore().getCertificateAlias(cert); 111 } 112 113 protected Certificate [] getCertificateChain(String alias) throws KeyStoreException { 114 return loadKeyStore().getCertificateChain(alias); 115 } 116 117 public PrivateKey getPrivateKey(String alias, String password) throws Exception { 118 password = keyPassword; 121 if (password == null) { 122 password = keyStorePassword; 123 } 124 if (alias == null) { 125 throw new Exception ("alias is null"); 126 } 127 KeyStore keystore = loadKeyStore(); 128 boolean b = keystore.isKeyEntry(alias); 129 if (!b) { 130 throw new Exception ("Cannot find key for alias: " + alias); 131 } 132 Key keyTmp = keystore.getKey(alias, (password == null || password.length() == 0) ? new char[0] : password.toCharArray()); 133 if (!(keyTmp instanceof PrivateKey )) { 134 throw new Exception ("Key is not a private key, alias: " + alias); 135 } 136 return (PrivateKey ) keyTmp; 137 } 138 139 protected String [] getTrustCertificates() throws KeyStoreException { 140 KeyStore keystore = loadKeyStore(); 141 Set hashSet = new HashSet (); 142 Enumeration aliases = keystore.aliases(); 143 while (aliases.hasMoreElements()) { 144 String alias = (String ) aliases.nextElement(); 145 if (keystore.isCertificateEntry(alias)) { 146 hashSet.add(alias); 147 } 148 } 149 return (String []) hashSet.toArray(new String [hashSet.size()]); 150 } 151 152 157 public synchronized KeyStore loadKeyStore() throws KeyStoreException { 158 if (keyStore != null) { 159 return keyStore; 160 } 161 if (keyStoreUrl == null) { 162 throw new IllegalArgumentException ("keyStoreUrl not specified in this StandaloneCrypto"); 163 } 164 InputStream input = null; 165 try { 166 input = keyStoreUrl.getInputStream(); 167 String provider = getProvider(); 168 String type = keyStoreType != null ? keyStoreType : KeyStore.getDefaultType(); 169 if (provider == null || provider.length() == 0) { 170 keyStore = KeyStore.getInstance(type); 171 } else { 172 keyStore = KeyStore.getInstance(type, provider); 173 } 174 keyStore.load(input, (keyStorePassword == null || keyStorePassword.length() == 0) ? new char[0] : keyStorePassword.toCharArray()); 175 return keyStore; 176 } catch (IOException e) { 177 throw new KeyStoreException (e); 178 } catch (GeneralSecurityException e) { 179 throw new KeyStoreException (e); 180 } catch (Exception e) { 181 throw new KeyStoreException (e); 182 } finally { 183 if (input != null) { 184 try { input.close(); } catch (Exception ignore) {} 185 } 186 } 187 } 188 189 } 190 | Popular Tags |