1 23 package com.sun.enterprise.security; 24 25 import java.io.BufferedInputStream ; 26 import java.io.FileInputStream ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import java.security.KeyStore ; 32 import java.security.Provider ; 33 34 import com.sun.enterprise.config.ConfigContext; 35 import com.sun.enterprise.security.SSLUtils; 36 import com.sun.enterprise.server.pluggable.SecuritySupport; 37 import com.sun.logging.LogDomains; 38 39 43 public class SecuritySupportImpl implements SecuritySupport { 44 45 private static final String keyStoreProp = "javax.net.ssl.keyStore"; 46 private static final String trustStoreProp = "javax.net.ssl.trustStore"; 47 48 private static boolean isInit = false; 49 protected static List keyStores = new ArrayList (); 50 protected static List trustStores = new ArrayList (); 51 protected static List keyStorePasswords = new ArrayList (); 52 protected static List tokenNames = new ArrayList (); 53 54 private static Logger _logger = null; 55 56 static { 57 _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 58 } 59 60 public SecuritySupportImpl() { 61 this(true); 62 } 63 64 protected SecuritySupportImpl(boolean initJKS) { 65 if (initJKS && !isInit) { 66 loadStores(null, KeyStore.getDefaultType(), null, 67 System.getProperty(keyStoreProp), SSLUtils.getKeyStorePass(), 68 System.getProperty(trustStoreProp), SSLUtils.getTrustStorePass()); 69 } 70 isInit = true; 71 } 72 73 84 protected synchronized static void loadStores(String tokenName, 85 String storeType, Provider provider, 86 String keyStoreFile, String keyStorePass, 87 String trustStoreFile, String trustStorePass) { 88 try { 89 KeyStore keyStore = loadKS(storeType, provider, keyStoreFile, 90 keyStorePass); 91 KeyStore trustStore = loadKS(storeType, provider,trustStoreFile, 92 trustStorePass); 93 keyStores.add(keyStore); 94 trustStores.add(trustStore); 95 keyStorePasswords.add(keyStorePass); 96 tokenNames.add(tokenName); 97 } catch(Exception ex) { 98 throw new IllegalStateException (ex.getMessage()); 99 } 100 } 101 102 112 private static KeyStore loadKS(String keyStoreType, Provider provider, 113 String keyStoreFile, String keyStorePass) 114 throws Exception 115 { 116 KeyStore ks = null; 117 if (provider != null) { 118 ks = KeyStore.getInstance(keyStoreType, provider); 119 } else { 120 ks = KeyStore.getInstance(keyStoreType); 121 } 122 char[] passphrase = keyStorePass.toCharArray(); 123 124 FileInputStream istream = null; 125 BufferedInputStream bstream = null; 126 try { 127 if (keyStoreFile != null) { 128 if (_logger.isLoggable(Level.FINE)) { 129 _logger.log(Level.FINE, "Loading keystoreFile = " + 130 keyStoreFile + ", keystorePass = " + keyStorePass); 131 } 132 istream = new FileInputStream (keyStoreFile); 133 bstream = new BufferedInputStream (istream); 134 } 135 136 ks.load(bstream, passphrase); 137 } finally { 138 if (bstream != null) { 139 bstream.close(); 140 } 141 if (istream != null) { 142 istream.close(); 143 } 144 } 145 return ks; 146 } 147 148 149 151 155 public KeyStore [] getKeyStores() { 156 return (KeyStore [])keyStores.toArray(new KeyStore [keyStores.size()]); 157 } 158 159 162 public KeyStore [] getTrustStores() { 163 return (KeyStore [])trustStores.toArray(new KeyStore [trustStores.size()]); 164 } 165 166 170 public String [] getKeyStorePasswords() { 171 return (String [])keyStorePasswords.toArray(new String [keyStorePasswords.size()]); 172 } 173 174 178 public String [] getTokenNames() { 179 return (String [])tokenNames.toArray(new String [tokenNames.size()]); 180 } 181 182 188 public void synchronizeKeyFile(ConfigContext configContext, 189 String fileRealmName) throws Exception { 190 } 192 193 197 public KeyStore getKeyStore(String token) { 198 int idx = getTokenIndex(token); 199 if (idx<0) return null; 200 return (KeyStore )keyStores.get(idx); 201 } 202 203 207 public KeyStore getTrustStore(String token) { 208 int idx = getTokenIndex(token); 209 if (idx<0) return null; 210 return (KeyStore )trustStores.get(idx); 211 } 212 213 217 public String getKeyStorePassword(String token) { 218 int idx = getTokenIndex(token); 219 if (idx<0) return null; 220 return (String )keyStorePasswords.get(idx); 221 } 222 223 226 private int getTokenIndex(String token) { 227 int idx = -1; 228 if (token!=null) { 229 idx = tokenNames.indexOf(token); 230 if (idx < 0) { 231 _logger.log(Level.WARNING,"token " + token + " is not found"); 232 if ( tokenNames.size() > 0 ) 233 idx = 0; 234 } 235 } 236 return idx; 237 } 238 } 239 | Popular Tags |