KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.security.cert.Certificate JavaDoc;
40 import javax.crypto.spec.SecretKeySpec;
41
42 import java.util.Enumeration JavaDoc;
43
44 /**
45  * This class implements an adapter for password manipulation a JCEKS.
46  * @author Shing Wai Chan
47  */

48 public class AsadminTruststore {
49     private static final String JavaDoc ASADMIN_TRUSTSTORE = ".asadmintruststore";
50     private KeyStore JavaDoc _keyStore = null;
51     private File JavaDoc _keyFile = null;
52     private char[] _password = null;
53       
54     public static File JavaDoc getAsadminTruststore()
55     {
56         String JavaDoc location = System.getProperty(SystemPropertyConstants.CLIENT_TRUSTSTORE_PROPERTY);
57         if (location == null) {
58             return new File JavaDoc(System.getProperty("user.home") + File.separator + ASADMIN_TRUSTSTORE);
59         } else {
60             return new File JavaDoc(location);
61         }
62     }
63     
64     public static String JavaDoc getAsadminTruststorePassword()
65     {
66         return System.getProperty(SystemPropertyConstants.CLIENT_TRUSTSTORE_PASSWORD_PROPERTY,
67             "changeit");
68     }
69     
70     public AsadminTruststore() throws CertificateException JavaDoc, IOException JavaDoc,
71         KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
72     {
73         this(getAsadminTruststorePassword());
74     }
75             
76     public AsadminTruststore(String JavaDoc password) throws CertificateException JavaDoc, IOException JavaDoc,
77         KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
78     {
79         init(getAsadminTruststore(), password);
80     }
81     
82     private void init(File JavaDoc keyfile, String JavaDoc password)
83         throws CertificateException JavaDoc, IOException JavaDoc,
84         KeyStoreException JavaDoc, NoSuchAlgorithmException JavaDoc
85     {
86         _keyFile = keyfile;
87         _keyStore = KeyStore.getInstance("JKS");
88         _password = password.toCharArray();
89         BufferedInputStream JavaDoc bInput = null;
90         if (_keyFile.exists()) {
91             bInput = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(_keyFile));
92         }
93         try {
94             //load must be called with null to initialize an empty keystore
95
_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 JavaDoc ex) {
105                      //ignore we are cleaning up
106
}
107              }
108         }
109     }
110     
111     public boolean certificateExists(Certificate JavaDoc cert) throws KeyStoreException JavaDoc
112     {
113         return (_keyStore.getCertificateAlias(cert) == null ? false : true);
114     }
115     
116     public void addCertificate(String JavaDoc alias, Certificate JavaDoc cert) throws KeyStoreException JavaDoc, IOException JavaDoc,
117         NoSuchAlgorithmException JavaDoc, CertificateException JavaDoc
118     {
119         _keyStore.setCertificateEntry(alias, cert);
120         writeStore();
121     }
122     
123     public void writeStore() throws KeyStoreException JavaDoc, IOException JavaDoc,
124         NoSuchAlgorithmException JavaDoc, CertificateException JavaDoc
125     {
126          BufferedOutputStream JavaDoc boutput = null;
127
128          try {
129              boutput = new BufferedOutputStream JavaDoc(
130                      new FileOutputStream JavaDoc(_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 JavaDoc ex) {
139                      //ignore we are cleaning up
140
}
141              }
142          }
143     }
144 }
145
Popular Tags