]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/membership/pse/URIKeyStoreManager.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / membership / pse / URIKeyStoreManager.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.membership.pse;
58
59
60 import java.io.File;
61 import java.io.FileOutputStream;
62 import java.io.OutputStream;
63 import java.net.URI;
64 import java.security.KeyStore;
65
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;
71
72 import java.util.logging.Level;
73 import net.jxta.logging.Logging;
74 import java.util.logging.Logger;
75
76
77 /**
78  *  Manages a Keystore located at URI. This version precludes KeyStores which
79  *  are built from multiple URIs.
80  **/
81 public class URIKeyStoreManager implements KeyStoreManager {
82     
83     /**
84      *  Log4J Logger
85      **/
86     private final static transient Logger LOG = Logger.getLogger(URIKeyStoreManager.class.getName());
87     
88     /**
89      *  The default keystore type we will use.
90      **/
91     private final static String DEFAULT_KEYSTORE_TYPE = "jks";
92     
93     /**
94      *  The keystore type
95      **/
96     private final String keystore_type;
97     
98     /**
99      *  The keystore type
100      **/
101     private final String keystore_provider;
102     
103     /**
104      *  The location where the keystore lives.
105      **/
106     private final URI keystore_location;
107     
108     /**
109      *  Default constructor.
110      **/
111     public URIKeyStoreManager(String type, String provider, URI location) throws NoSuchProviderException, KeyStoreException {
112         if (null == type) {
113             type = DEFAULT_KEYSTORE_TYPE;
114             provider = null;
115         }
116         
117         if (!location.isAbsolute()) {
118             throw new IllegalArgumentException("location must be an absolute URI");
119         }
120         
121         if ("file".equalsIgnoreCase(location.getScheme())) {
122             File asFile = new File(location);
123             
124             if (asFile.exists() && !asFile.isFile()) {
125                 throw new IllegalArgumentException("location must refer to a file");
126             }
127         }
128         
129         if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
130             LOG.config("pse location = " + location);
131         }
132         
133         keystore_type = type;
134         
135         keystore_provider = provider;
136         
137         keystore_location = location;
138         
139         // check if we can get an instance.
140         if (null == keystore_provider) {
141             KeyStore.getInstance(keystore_type);
142         } else {
143             KeyStore.getInstance(keystore_type, keystore_provider);
144         }
145     }
146     
147     /**
148      *  {@inheritDoc}
149      **/
150     public boolean isInitialized() {
151         return isInitialized(null);
152     }
153     
154     /**
155      *  {@inheritDoc}
156      **/
157     public boolean isInitialized(char[] store_password) {
158         try {
159             KeyStore store;
160
161             if (null == keystore_provider) {
162                 store = KeyStore.getInstance(keystore_type);
163             } else {
164                 store = KeyStore.getInstance(keystore_type, keystore_provider);
165             }
166             
167             store.load(keystore_location.toURL().openStream(), store_password);
168             
169             return true;
170         } catch (Exception failed) {
171             return false;
172         }
173     }
174     
175     /**
176      *  {@inheritDoc}
177      **/
178     public void createKeyStore(char[] store_password) throws KeyStoreException, IOException {
179         try {
180             KeyStore store;
181
182             if (null == keystore_provider) {
183                 store = KeyStore.getInstance(keystore_type);
184             } else {
185                 store = KeyStore.getInstance(keystore_type, keystore_provider);
186             }
187             
188             store.load(null, store_password);
189             
190             saveKeyStore(store, store_password);
191         } catch (NoSuchProviderException failed) {
192             KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
193
194             failure.initCause(failed);
195             throw failure;
196         } catch (NoSuchAlgorithmException failed) {
197             KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
198
199             failure.initCause(failed);
200             throw failure;
201         } catch (CertificateException failed) {
202             KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
203
204             failure.initCause(failed);
205             throw failure;
206         }
207     }
208     
209     /**
210      *  {@inheritDoc}
211      **/
212     public KeyStore loadKeyStore(char[] password) throws KeyStoreException, IOException {
213         
214         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
215             LOG.fine("Loading (" + keystore_type + "," + keystore_provider + ") store from " + keystore_location);
216         }
217         
218         try {
219             KeyStore store;
220
221             if (null == keystore_provider) {
222                 store = KeyStore.getInstance(keystore_type);
223             } else {
224                 store = KeyStore.getInstance(keystore_type, keystore_provider);
225             }
226             
227             store.load(keystore_location.toURL().openStream(), password);
228             
229             return store;
230         } catch (NoSuchAlgorithmException failed) {
231             KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
232
233             failure.initCause(failed);
234             throw failure;
235         } catch (CertificateException failed) {
236             KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
237
238             failure.initCause(failed);
239             throw failure;
240         } catch (NoSuchProviderException failed) {
241             KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
242
243             failure.initCause(failed);
244             throw failure;
245         }
246     }
247     
248     /**
249      *  {@inheritDoc}
250      **/
251     public void saveKeyStore(KeyStore store, char[] password) throws KeyStoreException, IOException {
252         
253         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
254             LOG.fine("Writing " + store + " to " + keystore_location);
255         }
256         
257         try {
258             OutputStream os = null;
259             
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));
264             } else {
265                 os = keystore_location.toURL().openConnection().getOutputStream();
266             }
267             store.store(os, password);
268         } catch (NoSuchAlgorithmException failed) {
269             KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
270
271             failure.initCause(failed);
272             throw failure;
273         } catch (CertificateException failed) {
274             KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
275
276             failure.initCause(failed);
277             throw failure;
278         }
279     }
280     
281     /**
282      *  {@inheritDoc}
283      **/
284     public void eraseKeyStore() {
285         
286         if ("file".equalsIgnoreCase(keystore_location.getScheme())) {
287             File asFile = new File(keystore_location);
288             
289             if (asFile.exists() && asFile.isFile() && asFile.canWrite()) {
290                 asFile.delete();
291             }
292         } else {
293             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
294                 LOG.severe("Unable to delete non-file URI :" + keystore_location);
295             }
296             
297             throw new UnsupportedOperationException("Unable to delete non-file URI");
298         }
299     }
300
301     /**
302      *  {@inheritDoc}
303      **/
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();
311    }
312
313 }