2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.impl.membership.pse;
60 import java.io.ByteArrayOutputStream;
61 import java.io.InputStream;
62 import java.security.KeyStore;
64 import java.io.IOException;
65 import java.security.KeyStoreException;
66 import java.security.NoSuchProviderException;
67 import java.security.NoSuchAlgorithmException;
68 import java.security.cert.CertificateException;
70 import java.util.logging.Level;
71 import net.jxta.logging.Logging;
72 import java.util.logging.Logger;
74 import net.jxta.id.ID;
75 import net.jxta.peergroup.PeerGroup;
77 import net.jxta.impl.cm.Cm;
78 import net.jxta.impl.peergroup.StdPeerGroup;
82 * Manages a Keystore located within the JXTA CM.
84 public class CMKeyStoreManager implements KeyStoreManager {
89 private final static transient Logger LOG = Logger.getLogger(CMKeyStoreManager.class.getName());
92 * Our default keystore type.
94 private final static String DEFAULT_KEYSTORE_TYPE = "jks";
99 private final String keystore_type;
104 private final String keystore_provider;
107 * The JXTA CM where the keystore lives.
109 private final Cm keystore_cm;
112 * The CM ID where the keystore lives.
114 private final ID keystore_location;
117 * Default constructor.
119 * @param type The keystore type to use. The current default is the "JKS"
120 * keystore which is specified via {@code null}.
121 * @param provider The JCE cryptographic provider to use for the keystore.
122 * May also be {@code null} for the default provider.
123 * @param group The peer group which will provide the CM.
124 * @param location The ID under which the keystore will be stored in the
126 * @throws NoSuchProviderException Thrown if the requested provider is not
128 * @throws KeyStoreException Thrown for errors getting a keystore of the
131 public CMKeyStoreManager(String type, String provider, PeerGroup group, ID location) throws NoSuchProviderException, KeyStoreException {
134 type = DEFAULT_KEYSTORE_TYPE;
138 keystore_type = type;
140 keystore_provider = provider;
142 keystore_cm = ((StdPeerGroup) group).getCacheManager();
144 keystore_location = location;
146 // check if we can get an instance.
147 if (null == keystore_provider) {
148 KeyStore.getInstance(keystore_type);
150 KeyStore.getInstance(keystore_type, keystore_provider);
153 if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
154 LOG.config("pse location = " + keystore_location + " in " + keystore_cm);
161 public boolean isInitialized() {
162 return isInitialized(null);
168 public boolean isInitialized(char[] store_password) {
172 if (null == keystore_provider) {
173 store = KeyStore.getInstance(keystore_type);
175 store = KeyStore.getInstance(keystore_type, keystore_provider);
178 InputStream is = keystore_cm.getInputStream("Raw", keystore_location.toString());
184 store.load(is, store_password);
187 } catch (Exception failed) {
195 public void createKeyStore(char[] store_password) throws KeyStoreException, IOException {
199 if (null == keystore_provider) {
200 store = KeyStore.getInstance(keystore_type);
202 store = KeyStore.getInstance(keystore_type, keystore_provider);
205 store.load(null, store_password);
207 saveKeyStore(store, store_password);
208 } catch (NoSuchProviderException failed) {
209 KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
211 failure.initCause(failed);
213 } catch (NoSuchAlgorithmException failed) {
214 KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
216 failure.initCause(failed);
218 } catch (CertificateException failed) {
219 KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
221 failure.initCause(failed);
229 public KeyStore loadKeyStore(char[] password) throws KeyStoreException, IOException {
231 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
232 LOG.fine("Loading (" + keystore_type + "," + keystore_provider + ") store from " + keystore_location);
238 if (null == keystore_provider) {
239 store = KeyStore.getInstance(keystore_type);
241 store = KeyStore.getInstance(keystore_type, keystore_provider);
244 InputStream is = keystore_cm.getInputStream("Raw", keystore_location.toString());
246 store.load(is, password);
249 } catch (NoSuchAlgorithmException failed) {
250 KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
252 failure.initCause(failed);
254 } catch (CertificateException failed) {
255 KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
257 failure.initCause(failed);
259 } catch (NoSuchProviderException failed) {
260 KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
262 failure.initCause(failed);
270 public void saveKeyStore(KeyStore store, char[] password) throws IOException, KeyStoreException {
272 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
273 LOG.fine("Writing " + store + " to " + keystore_location);
277 ByteArrayOutputStream bos = new ByteArrayOutputStream();
279 store.store(bos, password);
282 keystore_cm.save("Raw", keystore_location.toString(), bos.toByteArray(), Long.MAX_VALUE, 0);
283 } catch (NoSuchAlgorithmException failed) {
284 KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
286 failure.initCause(failed);
288 } catch (CertificateException failed) {
289 KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
291 failure.initCause(failed);
299 public void eraseKeyStore() throws IOException {
301 keystore_cm.remove("Raw", keystore_location.toString());
307 public String toString() {
308 StringBuilder sb = new StringBuilder("PSE keystore details: \n");
309 sb.append(" Class: ").append(this.getClass().getName()).append("\n");
310 sb.append(" Type: ").append(keystore_type==null ? "<default>" : keystore_type).append("\n");
311 sb.append(" Provider: ").append(keystore_provider==null ? "<default>" : keystore_provider).append("\n");
312 sb.append(" Location: ").append(keystore_location==null ? "<default>" : keystore_location.toString()).append("\n");
313 return sb.toString();