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 java.security.cert.Certificate ; 40 import javax.crypto.spec.SecretKeySpec; 41 42 import java.util.Enumeration ; 43 44 48 public class AsadminTruststore { 49 private static final String ASADMIN_TRUSTSTORE = ".asadmintruststore"; 50 private KeyStore _keyStore = null; 51 private File _keyFile = null; 52 private char[] _password = null; 53 54 public static File getAsadminTruststore() 55 { 56 String location = System.getProperty(SystemPropertyConstants.CLIENT_TRUSTSTORE_PROPERTY); 57 if (location == null) { 58 return new File (System.getProperty("user.home") + File.separator + ASADMIN_TRUSTSTORE); 59 } else { 60 return new File (location); 61 } 62 } 63 64 public static String getAsadminTruststorePassword() 65 { 66 return System.getProperty(SystemPropertyConstants.CLIENT_TRUSTSTORE_PASSWORD_PROPERTY, 67 "changeit"); 68 } 69 70 public AsadminTruststore() throws CertificateException , IOException , 71 KeyStoreException , NoSuchAlgorithmException 72 { 73 this(getAsadminTruststorePassword()); 74 } 75 76 public AsadminTruststore(String password) throws CertificateException , IOException , 77 KeyStoreException , NoSuchAlgorithmException 78 { 79 init(getAsadminTruststore(), password); 80 } 81 82 private void init(File keyfile, String password) 83 throws CertificateException , IOException , 84 KeyStoreException , NoSuchAlgorithmException 85 { 86 _keyFile = keyfile; 87 _keyStore = KeyStore.getInstance("JKS"); 88 _password = password.toCharArray(); 89 BufferedInputStream bInput = null; 90 if (_keyFile.exists()) { 91 bInput = new BufferedInputStream (new FileInputStream (_keyFile)); 92 } 93 try { 94 _keyStore.load(bInput, _password); 96 if (bInput != null) { 97 bInput.close(); 98 bInput = null; 99 } 100 } finally { 101 if (bInput != null) { 102 try { 103 bInput.close(); 104 } catch(Exception ex) { 105 } 107 } 108 } 109 } 110 111 public boolean certificateExists(Certificate cert) throws KeyStoreException 112 { 113 return (_keyStore.getCertificateAlias(cert) == null ? false : true); 114 } 115 116 public void addCertificate(String alias, Certificate cert) throws KeyStoreException , IOException , 117 NoSuchAlgorithmException , CertificateException 118 { 119 _keyStore.setCertificateEntry(alias, cert); 120 writeStore(); 121 } 122 123 public void writeStore() throws KeyStoreException , IOException , 124 NoSuchAlgorithmException , CertificateException 125 { 126 BufferedOutputStream boutput = null; 127 128 try { 129 boutput = new BufferedOutputStream ( 130 new FileOutputStream (_keyFile)); 131 _keyStore.store(boutput, _password); 132 boutput.close(); 133 boutput = null; 134 } finally { 135 if (boutput != null) { 136 try { 137 boutput.close(); 138 } catch(Exception ex) { 139 } 141 } 142 } 143 } 144 } 145 | Popular Tags |