1 23 package com.sun.enterprise.admin.jmx.remote.https; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.BufferedInputStream ; 28 import java.io.BufferedOutputStream ; 29 import java.io.IOException ; 30 import java.security.Key ; 31 import java.security.KeyStore ; 32 import java.security.KeyStoreException ; 33 import java.security.NoSuchAlgorithmException ; 34 import java.security.UnrecoverableKeyException ; 35 import java.security.cert.CertificateException ; 36 import java.security.cert.Certificate ; 37 import javax.crypto.spec.SecretKeySpec; 38 39 import java.util.Enumeration ; 40 41 45 public class AsadminTruststore { 46 private static final String ASADMIN_TRUSTSTORE = ".asadmintruststore"; 47 private KeyStore _keyStore = null; 48 private File _keyFile = null; 49 private char[] _password = null; 50 51 public static final String CLIENT_TRUSTSTORE_PROPERTY = 52 "javax.net.ssl.trustStore"; 53 public static final String CLIENT_TRUSTSTORE_PASSWORD_PROPERTY = 54 "javax.net.ssl.trustStorePassword"; 55 56 public static File getAsadminTruststore() 57 { 58 String location = System.getProperty(CLIENT_TRUSTSTORE_PROPERTY); 59 if (location == null) { 60 return new File (System.getProperty("user.home") + File.separator + ASADMIN_TRUSTSTORE); 61 } else { 62 return new File (location); 63 } 64 } 65 66 public static String getAsadminTruststorePassword() 67 { 68 return System.getProperty(CLIENT_TRUSTSTORE_PASSWORD_PROPERTY, 69 "changeit"); 70 } 71 72 public AsadminTruststore() throws CertificateException , IOException , 73 KeyStoreException , NoSuchAlgorithmException 74 { 75 this(getAsadminTruststorePassword()); 76 } 77 78 public AsadminTruststore(String password) throws CertificateException , IOException , 79 KeyStoreException , NoSuchAlgorithmException 80 { 81 init(getAsadminTruststore(), password); 82 } 83 84 private void init(File keyfile, String password) 85 throws CertificateException , IOException , 86 KeyStoreException , NoSuchAlgorithmException 87 { 88 _keyFile = keyfile; 89 _keyStore = KeyStore.getInstance("JKS"); 90 _password = password.toCharArray(); 91 BufferedInputStream bInput = null; 92 if (_keyFile.exists()) { 93 bInput = new BufferedInputStream (new FileInputStream (_keyFile)); 94 } 95 try { 96 _keyStore.load(bInput, _password); 98 if (bInput != null) { 99 bInput.close(); 100 bInput = null; 101 } 102 } finally { 103 if (bInput != null) { 104 try { 105 bInput.close(); 106 } catch(Exception ex) { 107 } 109 } 110 } 111 } 112 113 public boolean certificateExists(Certificate cert) throws KeyStoreException 114 { 115 return (_keyStore.getCertificateAlias(cert) == null ? false : true); 116 } 117 118 public void addCertificate(String alias, Certificate cert) throws KeyStoreException , IOException , 119 NoSuchAlgorithmException , CertificateException 120 { 121 _keyStore.setCertificateEntry(alias, cert); 122 writeStore(); 123 } 124 125 public void writeStore() throws KeyStoreException , IOException , 126 NoSuchAlgorithmException , CertificateException 127 { 128 BufferedOutputStream boutput = null; 129 130 try { 131 boutput = new BufferedOutputStream ( 132 new FileOutputStream (_keyFile)); 133 _keyStore.store(boutput, _password); 134 boutput.close(); 135 boutput = null; 136 } finally { 137 if (boutput != null) { 138 try { 139 boutput.close(); 140 } catch(Exception ex) { 141 } 143 } 144 } 145 } 146 } 147 | Popular Tags |