1 23 package com.sun.enterprise.security.store; 24 25 import com.sun.enterprise.util.SystemPropertyConstants; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.BufferedInputStream ; 31 import java.io.BufferedOutputStream ; 32 import java.io.IOException ; 33 import java.security.Key ; 34 import java.security.KeyStore ; 35 import java.security.KeyStoreException ; 36 import java.security.NoSuchAlgorithmException ; 37 import java.security.UnrecoverableKeyException ; 38 import java.security.cert.CertificateException ; 39 import javax.crypto.spec.SecretKeySpec; 40 41 import java.util.Enumeration ; 42 43 47 public class PasswordAdapter { 48 public static final String PASSWORD_ALIAS_KEYSTORE = "domain-passwords"; 49 private KeyStore _pwdStore = null; 50 private String _keyFile = null; 51 private char[] _masterPassword = null; 52 53 private char[] getMasterPassword() { 54 return _masterPassword; 55 } 56 57 private void setMasterPassword(char[] smp) { 58 _masterPassword = smp; 59 } 60 61 70 public PasswordAdapter(char[] smp) 71 throws CertificateException , IOException , 72 KeyStoreException , NoSuchAlgorithmException 73 { 74 75 String keyfileName = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY) + 76 File.separator + "config" + File.separator + PASSWORD_ALIAS_KEYSTORE; 77 init(keyfileName, smp); 78 } 79 80 90 public PasswordAdapter(String keyfileName, char[] smp) 91 throws CertificateException , IOException , 92 KeyStoreException , NoSuchAlgorithmException 93 { 94 init(keyfileName, smp); 95 } 96 97 107 private void init(String keyfileName, char[] smp) 108 throws CertificateException , IOException , 109 KeyStoreException , NoSuchAlgorithmException 110 { 111 _keyFile = keyfileName; 112 _pwdStore = KeyStore.getInstance("JCEKS"); 113 setMasterPassword(smp); 114 BufferedInputStream bInput = null; 115 File file = new File (keyfileName); 116 if (file.exists()) { 117 bInput = new BufferedInputStream (new FileInputStream (file)); 118 } 119 try { 120 _pwdStore.load(bInput, getMasterPassword()); 122 if (bInput != null) { 123 bInput.close(); 124 bInput = null; 125 } 126 } finally { 127 if (bInput != null) { 128 try { 129 bInput.close(); 130 } catch(Exception ex) { 131 } 133 } 134 } 135 } 136 137 145 public String getPasswordForAlias(String alias) 146 throws KeyStoreException , NoSuchAlgorithmException , 147 UnrecoverableKeyException { 148 149 Key key = _pwdStore.getKey(alias, getMasterPassword()); 150 if (key != null) { 151 return new String (key.getEncoded()); 152 } else { 153 return null; 154 } 155 } 156 157 162 public boolean aliasExists(String alias) throws KeyStoreException 163 { 164 return _pwdStore.containsAlias(alias); 165 } 166 167 175 public void removeAlias(String alias) throws KeyStoreException , IOException , 176 NoSuchAlgorithmException , CertificateException 177 { 178 _pwdStore.deleteEntry(alias); 179 writeStore(); 180 } 181 182 186 public Enumeration getAliases() throws KeyStoreException 187 { 188 return _pwdStore.aliases(); 189 } 190 191 198 public void writeStore() throws KeyStoreException , IOException , 199 NoSuchAlgorithmException , CertificateException 200 { 201 BufferedOutputStream boutput = null; 202 203 try { 204 boutput = new BufferedOutputStream ( 205 new FileOutputStream (_keyFile)); 206 _pwdStore.store(boutput, getMasterPassword()); 207 boutput.close(); 208 boutput = null; 209 } finally { 210 if (boutput != null) { 211 try { 212 boutput.close(); 213 } catch(Exception ex) { 214 } 216 } 217 } 218 } 219 220 229 public void setPasswordForAlias(String alias, byte[] secretKey) 230 throws CertificateException , IOException , KeyStoreException , 231 NoSuchAlgorithmException 232 { 233 Key key = new SecretKeySpec(secretKey, "AES"); 234 _pwdStore.setKeyEntry(alias, key, getMasterPassword(), null); 235 writeStore(); 236 } 237 238 246 public void changePassword(char[] newpassword) throws KeyStoreException , IOException , 247 NoSuchAlgorithmException , CertificateException 248 { 249 setMasterPassword(newpassword); 250 writeStore(); 251 } 252 } 253 | Popular Tags |