KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > SecuritySupportImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.security;
24
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.security.KeyStore JavaDoc;
32 import java.security.Provider JavaDoc;
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 /**
40  * This implements SecuritySupport used in PluggableFeatureFactory.
41  * @author Shing Wai Chan
42  */

43 public class SecuritySupportImpl implements SecuritySupport {
44
45     private static final String JavaDoc keyStoreProp = "javax.net.ssl.keyStore";
46     private static final String JavaDoc trustStoreProp = "javax.net.ssl.trustStore";
47
48     private static boolean isInit = false;
49     protected static List JavaDoc keyStores = new ArrayList JavaDoc();
50     protected static List JavaDoc trustStores = new ArrayList JavaDoc();
51     protected static List JavaDoc keyStorePasswords = new ArrayList JavaDoc();
52     protected static List JavaDoc tokenNames = new ArrayList JavaDoc();
53
54     private static Logger JavaDoc _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     /**
74      * This method will load keystore and truststore and add into
75      * corresponding list.
76      * @param tokenName
77      * @param storeType
78      * @param provider
79      * @param keyStorePass
80      * @param keyStoreFile
81      * @param trustStorePass
82      * @param trustStoreFile
83      */

84     protected synchronized static void loadStores(String JavaDoc tokenName,
85             String JavaDoc storeType, Provider JavaDoc provider,
86             String JavaDoc keyStoreFile, String JavaDoc keyStorePass,
87             String JavaDoc trustStoreFile, String JavaDoc trustStorePass) {
88         try {
89             KeyStore JavaDoc keyStore = loadKS(storeType, provider, keyStoreFile,
90                 keyStorePass);
91             KeyStore JavaDoc 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 JavaDoc ex) {
98             throw new IllegalStateException JavaDoc(ex.getMessage());
99         }
100     }
101
102     /**
103      * This method load keystore with given keystore file and
104      * keystore password for a given keystore type and provider.
105      * It always return a non-null keystore.
106      * @param keyStoreType
107      * @param provider
108      * @param keyStoreFile
109      * @param keyStorePass
110      * @retun keystore loaded
111      */

112     private static KeyStore JavaDoc loadKS(String JavaDoc keyStoreType, Provider JavaDoc provider,
113             String JavaDoc keyStoreFile, String JavaDoc keyStorePass)
114             throws Exception JavaDoc
115     {
116         KeyStore JavaDoc 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 JavaDoc istream = null;
125         BufferedInputStream JavaDoc 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 JavaDoc(keyStoreFile);
133                 bstream = new BufferedInputStream JavaDoc(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     // --- implements SecuritySupport ---
150

151     /**
152      * This method returns an array of keystores containing keys and
153      * certificates.
154      */

155     public KeyStore JavaDoc[] getKeyStores() {
156         return (KeyStore JavaDoc[])keyStores.toArray(new KeyStore JavaDoc[keyStores.size()]);
157     }
158
159     /**
160      * This method returns an array of truststores containing certificates.
161      */

162     public KeyStore JavaDoc[] getTrustStores() {
163         return (KeyStore JavaDoc[])trustStores.toArray(new KeyStore JavaDoc[trustStores.size()]);
164     }
165
166     /**
167      * This method returns an array of passwords in order corresponding to
168      * array of keystores.
169      */

170     public String JavaDoc[] getKeyStorePasswords() {
171         return (String JavaDoc[])keyStorePasswords.toArray(new String JavaDoc[keyStorePasswords.size()]);
172     }
173
174     /**
175      * This method returns an array of token names in order corresponding to
176      * array of keystores.
177      */

178     public String JavaDoc[] getTokenNames() {
179         return (String JavaDoc[])tokenNames.toArray(new String JavaDoc[tokenNames.size()]);
180     }
181
182     /**
183      * This method synchronize key file for given realm.
184      * @param configContext the ConfigContextx
185      * @param fileRealmName
186      * @exception
187      */

188     public void synchronizeKeyFile(ConfigContext configContext,
189             String JavaDoc fileRealmName) throws Exception JavaDoc {
190         // no op
191
}
192
193     /**
194      * @param token
195      * @return a keystore
196      */

197     public KeyStore JavaDoc getKeyStore(String JavaDoc token) {
198         int idx = getTokenIndex(token);
199         if (idx<0) return null;
200         return (KeyStore JavaDoc)keyStores.get(idx);
201     }
202
203     /**
204      * @param token
205      * @return a truststore
206      */

207     public KeyStore JavaDoc getTrustStore(String JavaDoc token) {
208         int idx = getTokenIndex(token);
209         if (idx<0) return null;
210         return (KeyStore JavaDoc)trustStores.get(idx);
211     }
212
213     /**
214      * @param token
215      * @return the password for this token
216      */

217     public String JavaDoc getKeyStorePassword(String JavaDoc token) {
218         int idx = getTokenIndex(token);
219         if (idx<0) return null;
220         return (String JavaDoc)keyStorePasswords.get(idx);
221     }
222    
223     /**
224      * @return returned index >= 0
225      */

226     private int getTokenIndex(String JavaDoc 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