KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > store > PasswordAdapter


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.store;
24
25 import com.sun.enterprise.util.SystemPropertyConstants;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.BufferedInputStream JavaDoc;
31 import java.io.BufferedOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.security.Key JavaDoc;
34 import java.security.KeyStore JavaDoc;
35 import java.security.KeyStoreException JavaDoc;
36 import java.security.NoSuchAlgorithmException JavaDoc;
37 import java.security.UnrecoverableKeyException JavaDoc;
38 import java.security.cert.CertificateException JavaDoc;
39 import javax.crypto.spec.SecretKeySpec;
40
41 import java.util.Enumeration JavaDoc;
42
43 /**
44  * This class implements an adapter for password manipulation a JCEKS.
45  * @author Shing Wai Chan
46  */

47 public class PasswordAdapter {
48     public static final String JavaDoc PASSWORD_ALIAS_KEYSTORE = "domain-passwords";
49     private KeyStore JavaDoc _pwdStore = null;
50     private String JavaDoc _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     /**
62      * Construct a PasswordAdapter with given Shared Master Password,
63      * SMP using the default keyfile (domain-passwords.jceks)
64      * @param smp master password
65      * @throws CertificateException
66      * @throws IOException
67      * @throws KeyStoreException
68      * @throws NoSuchAlgorithmException
69      */

70     public PasswordAdapter(char[] smp)
71             throws CertificateException JavaDoc, IOException JavaDoc,
72             KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
73     {
74         
75         String JavaDoc keyfileName = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY) +
76             File.separator + "config" + File.separator + PASSWORD_ALIAS_KEYSTORE;
77         init(keyfileName, smp);
78     }
79     
80     /**
81      * Construct a PasswordAdapter with given Shared Master Password,
82      * SMP.
83      * @param keyfileName the jceks key file name
84      * @param smp master password
85      * @throws CertificateException
86      * @throws IOException
87      * @throws KeyStoreException
88      * @throws NoSuchAlgorithmException
89      */

90     public PasswordAdapter(String JavaDoc keyfileName, char[] smp)
91             throws CertificateException JavaDoc, IOException JavaDoc,
92             KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
93     {
94         init(keyfileName, smp);
95     }
96     
97     /**
98      * Construct a PasswordAdapter with given Shared Master Password,
99      * SMP.
100      * @param keyfileName the jceks key file name
101      * @param smp the master password
102      * @exception CertificateException
103      * @exception IOException
104      * @exception KeyStoreException
105      * @exception NoSuchAlgorithmException
106      */

107     private void init(String JavaDoc keyfileName, char[] smp)
108             throws CertificateException JavaDoc, IOException JavaDoc,
109             KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
110     {
111         _keyFile = keyfileName;
112         _pwdStore = KeyStore.getInstance("JCEKS");
113         setMasterPassword(smp);
114         BufferedInputStream JavaDoc bInput = null;
115         File JavaDoc file = new File JavaDoc(keyfileName);
116         if (file.exists()) {
117             bInput = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
118         }
119         try {
120             //load must be called with null to initialize an empty keystore
121
_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 JavaDoc ex) {
131                      //ignore we are cleaning up
132
}
133              }
134         }
135     }
136     
137     /**
138      * This methods returns password String for a given alias and SMP.
139      * @param alias
140      * @return corresponding password or null if the alias does not exist.
141      * @exception KeyStoreException
142      * @exception NoSuchAlgorithmException
143      * @exception UnrecoverableKeyException
144      */

145     public String JavaDoc getPasswordForAlias(String JavaDoc alias)
146             throws KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc,
147             UnrecoverableKeyException JavaDoc {
148
149          Key JavaDoc key = _pwdStore.getKey(alias, getMasterPassword());
150          if (key != null) {
151             return new String JavaDoc(key.getEncoded());
152          } else {
153              return null;
154          }
155     }
156
157     /**
158      * See if the given alias exists
159      * @param alias the alias name
160      * @return true if the alias exists in the keystore
161      */

162     public boolean aliasExists(String JavaDoc alias) throws KeyStoreException JavaDoc
163     {
164         return _pwdStore.containsAlias(alias);
165     }
166     
167     /**
168      * Remove an alias from the keystore
169      * @param alias The name of the alias to remove
170      * @throws KeyStoreException
171      * @throws IOException
172      * @throws NoSuchAlgorithmException
173      * @throws CertificateException
174      */

175     public void removeAlias(String JavaDoc alias) throws KeyStoreException JavaDoc, IOException JavaDoc,
176         NoSuchAlgorithmException JavaDoc, CertificateException JavaDoc
177     {
178         _pwdStore.deleteEntry(alias);
179         writeStore();
180     }
181     
182     /**
183      * Return the aliases from the keystore.
184      * @return An enumeration containing all the aliases in the keystore.
185      */

186     public Enumeration JavaDoc getAliases() throws KeyStoreException JavaDoc
187     {
188         return _pwdStore.aliases();
189     }
190     
191     /**
192      * Writes the keystore to disk
193      * @throws KeyStoreException
194      * @throws IOException
195      * @throws NoSuchAlgorithmException
196      * @throws CertificateException
197      */

198     public void writeStore() throws KeyStoreException JavaDoc, IOException JavaDoc,
199         NoSuchAlgorithmException JavaDoc, CertificateException JavaDoc
200     {
201          BufferedOutputStream JavaDoc boutput = null;
202
203          try {
204              boutput = new BufferedOutputStream JavaDoc(
205                      new FileOutputStream JavaDoc(_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 JavaDoc ex) {
214                      //ignore we are cleaning up
215
}
216              }
217          }
218     }
219     
220     /**
221      * This methods set alias, secretKey into JCEKS keystore.
222      * @param alias
223      * @param secretKey
224      * @exception CertificateException
225      * @exception IOException
226      * @exception KeyStoreException
227      * @exception NoSuchAlgorithmException
228      */

229     public void setPasswordForAlias(String JavaDoc alias, byte[] secretKey)
230         throws CertificateException JavaDoc, IOException JavaDoc, KeyStoreException JavaDoc,
231         NoSuchAlgorithmException JavaDoc
232     {
233          Key JavaDoc key = new SecretKeySpec(secretKey, "AES");
234          _pwdStore.setKeyEntry(alias, key, getMasterPassword(), null);
235          writeStore();
236     }
237     
238     /**
239      * Changes the keystore password
240      * @param newpassword the new keystore password
241      * @throws KeyStoreException
242      * @throws IOException
243      * @throws NoSuchAlgorithmException
244      * @throws CertificateException
245      */

246     public void changePassword(char[] newpassword) throws KeyStoreException JavaDoc, IOException JavaDoc,
247         NoSuchAlgorithmException JavaDoc, CertificateException JavaDoc
248     {
249         setMasterPassword(newpassword);
250         writeStore();
251     }
252 }
253
Popular Tags