]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/membership/pse/CMKeyStoreManager.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 / CMKeyStoreManager.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.ByteArrayOutputStream;
61 import java.io.InputStream;
62 import java.security.KeyStore;
63
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;
69
70 import java.util.logging.Level;
71 import net.jxta.logging.Logging;
72 import java.util.logging.Logger;
73
74 import net.jxta.id.ID;
75 import net.jxta.peergroup.PeerGroup;
76
77 import net.jxta.impl.cm.Cm;
78 import net.jxta.impl.peergroup.StdPeerGroup;
79
80
81 /**
82  *  Manages a Keystore located within the JXTA CM.
83  **/
84 public class CMKeyStoreManager implements KeyStoreManager {
85     
86     /**
87      *  Log4J Logger
88      **/
89     private final static transient Logger LOG = Logger.getLogger(CMKeyStoreManager.class.getName());
90     
91     /**
92      *  Our default keystore type.
93      **/
94     private final static String DEFAULT_KEYSTORE_TYPE = "jks";
95     
96     /**
97      *  The keystore type
98      **/
99     private final String keystore_type;
100     
101     /**
102      *  The keystore type
103      **/
104     private final String keystore_provider;
105     
106     /**
107      *  The JXTA CM where the keystore lives.
108      **/
109     private final Cm keystore_cm;
110     
111     /**
112      *  The CM ID where the keystore lives.
113      **/
114     private final ID keystore_location;
115     
116     /**
117      *  Default constructor.
118      *
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
125      *  CM.
126      *  @throws NoSuchProviderException Thrown if the requested provider is not
127      *  available.
128      *  @throws KeyStoreException Thrown for errors getting a keystore of the
129      *  requested type.
130      **/
131     public CMKeyStoreManager(String type, String provider, PeerGroup group, ID location) throws NoSuchProviderException, KeyStoreException {
132         
133         if (null == type) {
134             type = DEFAULT_KEYSTORE_TYPE;
135             provider = null;
136         }
137         
138         keystore_type = type;
139         
140         keystore_provider = provider;
141         
142         keystore_cm = ((StdPeerGroup) group).getCacheManager();
143         
144         keystore_location = location;
145         
146         // check if we can get an instance.
147         if (null == keystore_provider) {
148             KeyStore.getInstance(keystore_type);
149         } else {
150             KeyStore.getInstance(keystore_type, keystore_provider);
151         }
152         
153         if (Logging.SHOW_CONFIG && LOG.isLoggable(Level.CONFIG)) {
154             LOG.config("pse location = " + keystore_location + " in " + keystore_cm);
155         }
156     }
157     
158     /**
159      *  {@inheritDoc}
160      **/
161     public boolean isInitialized() {
162         return isInitialized(null);
163     }
164     
165     /**
166      *  {@inheritDoc}
167      **/
168     public boolean isInitialized(char[] store_password) {
169         try {
170             KeyStore store;
171
172             if (null == keystore_provider) {
173                 store = KeyStore.getInstance(keystore_type);
174             } else {
175                 store = KeyStore.getInstance(keystore_type, keystore_provider);
176             }
177             
178             InputStream is = keystore_cm.getInputStream("Raw", keystore_location.toString());
179             
180             if (null == is) {
181                 return false;
182             }
183             
184             store.load(is, store_password);
185             
186             return true;
187         } catch (Exception failed) {
188             return false;
189         }
190     }
191     
192     /**
193      *  {@inheritDoc}
194      **/
195     public void createKeyStore(char[] store_password) throws KeyStoreException, IOException {
196         try {
197             KeyStore store;
198
199             if (null == keystore_provider) {
200                 store = KeyStore.getInstance(keystore_type);
201             } else {
202                 store = KeyStore.getInstance(keystore_type, keystore_provider);
203             }
204             
205             store.load(null, store_password);
206             
207             saveKeyStore(store, store_password);
208         } catch (NoSuchProviderException failed) {
209             KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
210
211             failure.initCause(failed);
212             throw failure;
213         } catch (NoSuchAlgorithmException failed) {
214             KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
215
216             failure.initCause(failed);
217             throw failure;
218         } catch (CertificateException failed) {
219             KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
220
221             failure.initCause(failed);
222             throw failure;
223         }
224     }
225     
226     /**
227      *  {@inheritDoc}
228      **/
229     public KeyStore loadKeyStore(char[] password) throws KeyStoreException, IOException {
230         
231         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
232             LOG.fine("Loading (" + keystore_type + "," + keystore_provider + ") store from " + keystore_location);
233         }
234         
235         try {
236             KeyStore store;
237
238             if (null == keystore_provider) {
239                 store = KeyStore.getInstance(keystore_type);
240             } else {
241                 store = KeyStore.getInstance(keystore_type, keystore_provider);
242             }
243             
244             InputStream is = keystore_cm.getInputStream("Raw", keystore_location.toString());
245             
246             store.load(is, password);
247             
248             return store;
249         } catch (NoSuchAlgorithmException failed) {
250             KeyStoreException failure = new KeyStoreException("NoSuchAlgorithmException during keystore processing");
251
252             failure.initCause(failed);
253             throw failure;
254         } catch (CertificateException failed) {
255             KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
256
257             failure.initCause(failed);
258             throw failure;
259         } catch (NoSuchProviderException failed) {
260             KeyStoreException failure = new KeyStoreException("NoSuchProviderException during keystore processing");
261
262             failure.initCause(failed);
263             throw failure;
264         }
265     }
266     
267     /**
268      *  {@inheritDoc}
269      **/
270     public void saveKeyStore(KeyStore store, char[] password) throws IOException, KeyStoreException {
271         
272         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
273             LOG.fine("Writing " + store + " to " + keystore_location);
274         }
275         
276         try {
277             ByteArrayOutputStream bos = new ByteArrayOutputStream();
278             
279             store.store(bos, password);
280             bos.close();
281             
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");
285
286             failure.initCause(failed);
287             throw failure;
288         } catch (CertificateException failed) {
289             KeyStoreException failure = new KeyStoreException("CertificateException during keystore processing");
290
291             failure.initCause(failed);
292             throw failure;
293         }
294     }
295     
296     /**
297      *  {@inheritDoc}
298      **/
299     public void eraseKeyStore() throws IOException {
300         
301         keystore_cm.remove("Raw", keystore_location.toString());
302     }
303
304     /**
305      *  {@inheritDoc}
306      **/
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();
314     }
315 }