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;
61 import java.io.FileOutputStream;
62 import java.io.OutputStream;
64 import java.security.KeyStore;
66 import java.io.IOException;
67 import java.security.KeyStoreException;
68 import java.security.NoSuchProviderException;
69 import java.security.NoSuchAlgorithmException;
70 import java.security.cert.CertificateException;
72 import java.util.logging.Level;
73 import net.jxta.logging.Logging;
74 import java.util.logging.Logger;
78 * Manages a Keystore located at URI. This version precludes KeyStores which
79 * are built from multiple URIs.
81 public class URIKeyStoreManager implements KeyStoreManager {
86 private final static transient Logger LOG = Logger.getLogger(URIKeyStoreManager.class.getName());
89 * The default keystore type we will use.
91 private final static String DEFAULT_KEYSTORE_TYPE = "jks";
96 private final String keystore_type;
101 private final String keystore_provider;
104 * The location where the keystore lives.
106 private final URI keystore_location;
109 * Default constructor.
111 public URIKeyStoreManager(String type, String provider, URI location) throws NoSuchProviderException, KeyStoreException {
113 type = DEFAULT_KEYSTORE_TYPE;
117 if (!location.isAbsolute()) {
118 throw new IllegalArgumentException("location must be an absolute URI");
121 if ("file".equalsIgnoreCase(location.getScheme())) {
122 File asFile = new File(location);
124 if (asFile.exists() && !asFile.isFile()) {
125 throw new IllegalArgumentException("location must refer to a file");
129 if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
130 LOG.config("pse location = " + location);
133 keystore_type = type;
135 keystore_provider = provider;
137 keystore_location = location;
139 // check if we can get an instance.
140 if (null == keystore_provider) {
141 KeyStore.getInstance(keystore_type);
143 KeyStore.getInstance(keystore_type, keystore_provider);
150 public boolean isInitialized() {
151 return isInitialized(null);
157 public boolean isInitialized(char[] store_password) {
161 if (null == keystore_provider) {
162 store = KeyStore.getInstance(keystore_type);
164 store = KeyStore.getInstance(keystore_type, keystore_provider);
167 store.load(keystore_location.toURL().openStream(), store_password);
170 } catch (Exception failed) {
178 public void createKeyStore(char[] store_password) throws KeyStoreException, IOException {
182 if (null == keystore_provider) {
183 store = KeyStore.getInstance(keystore_type);
185 store = KeyStore.getInstance(keystore_type, keystore_provider);
188 store.load(null, store_password);
190 saveKeyStore(store, store_password);
191 } catch (NoSuchProviderException failed) {
192 KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
194 failure.initCause(failed);
196 } catch (NoSuchAlgorithmException failed) {
197 KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
199 failure.initCause(failed);
201 } catch (CertificateException failed) {
202 KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
204 failure.initCause(failed);
212 public KeyStore loadKeyStore(char[] password) throws KeyStoreException, IOException {
214 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
215 LOG.fine("Loading (" + keystore_type + "," + keystore_provider + ") store from " + keystore_location);
221 if (null == keystore_provider) {
222 store = KeyStore.getInstance(keystore_type);
224 store = KeyStore.getInstance(keystore_type, keystore_provider);
227 store.load(keystore_location.toURL().openStream(), password);
230 } catch (NoSuchAlgorithmException failed) {
231 KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
233 failure.initCause(failed);
235 } catch (CertificateException failed) {
236 KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
238 failure.initCause(failed);
240 } catch (NoSuchProviderException failed) {
241 KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
243 failure.initCause(failed);
251 public void saveKeyStore(KeyStore store, char[] password) throws KeyStoreException, IOException {
253 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
254 LOG.fine("Writing " + store + " to " + keystore_location);
258 OutputStream os = null;
260 if ("file".equalsIgnoreCase(keystore_location.getScheme())) {
261 // Sadly we can't use URL.openConnection() to create the
262 // OutputStream for file:// URLs. bogus.
263 os = new FileOutputStream(new File(keystore_location));
265 os = keystore_location.toURL().openConnection().getOutputStream();
267 store.store(os, password);
268 } catch (NoSuchAlgorithmException failed) {
269 KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
271 failure.initCause(failed);
273 } catch (CertificateException failed) {
274 KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
276 failure.initCause(failed);
284 public void eraseKeyStore() {
286 if ("file".equalsIgnoreCase(keystore_location.getScheme())) {
287 File asFile = new File(keystore_location);
289 if (asFile.exists() && asFile.isFile() && asFile.canWrite()) {
293 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
294 LOG.severe("Unable to delete non-file URI :" + keystore_location);
297 throw new UnsupportedOperationException("Unable to delete non-file URI");
304 public String toString() {
305 StringBuilder sb = new StringBuilder("PSE keystore details: \n");
306 sb.append(" Class: ").append(this.getClass().getName()).append("\n");
307 sb.append(" Type: ").append(keystore_type==null ? "<default>" : keystore_type).append("\n");
308 sb.append(" Provider: ").append(keystore_provider==null ? "<default>" : keystore_provider).append("\n");
309 sb.append(" Location: ").append(keystore_location==null ? "<default>" : keystore_location.toString()).append("\n");
310 return sb.toString();