KickJava   Java API By Example, From Geeks To Geeks.

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

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