]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/membership/pse/EngineAuthenticator.java
37720c12bcec8ed08f00a30fffc92663ba74c8aa
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / membership / pse / EngineAuthenticator.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.net.URI;
61 import java.util.Arrays;
62 import java.util.ArrayList;
63 import java.util.Iterator;
64 import java.util.List;
65 import java.security.cert.X509Certificate;
66 import javax.crypto.EncryptedPrivateKeyInfo;
67
68 import java.io.IOException;
69 import java.net.URISyntaxException;
70 import java.security.KeyStoreException;
71
72 import java.util.logging.Level;
73 import net.jxta.logging.Logging;
74 import java.util.logging.Logger;
75
76 import net.jxta.credential.AuthenticationCredential;
77 import net.jxta.id.ID;
78 import net.jxta.id.IDFactory;
79 import net.jxta.peer.PeerID;
80 import net.jxta.membership.Authenticator;
81 import net.jxta.membership.MembershipService;
82
83
84 /**
85  * An authenticator associated with the PSE membership service.
86  *
87  *@see net.jxta.membership.Authenticator
88  *@see net.jxta.membership.MembershipService
89  **/
90 public class EngineAuthenticator implements Authenticator {
91     
92     /**
93      *  Log4J Logger
94      */
95     private static final Logger LOG = Logger.getLogger(EngineAuthenticator.class.getName());
96     
97     /**
98      * The Membership Service which generated this authenticator.
99      **/
100     transient PSEMembershipService source;
101     
102     /**
103      * The Authentication which was provided to the Apply operation of the
104      * membership service.
105      **/
106     transient AuthenticationCredential application;
107     
108     /**
109      *
110      **/
111     transient X509Certificate seedCert;
112     
113     /**
114      *
115      **/
116     transient PSEAuthenticatorEngine authenticatorEngine;
117     
118     /**
119      *
120      **/
121     // transient EncryptedPrivateKeyInfo seedKey;
122     
123     /**
124      * the password for that identity.
125      **/
126     transient char[] store_password = null;
127     
128     /**
129      * the identity which is being claimed
130      **/
131     transient ID identity = null;
132     
133     /**
134      * the password for that identity.
135      **/
136     transient char[] key_password = null;
137     
138     /**
139      * Creates an authenticator for the PSE membership service. Anything entered
140      * into the identity info section of the Authentication credential is
141      * ignored.
142      *
143      *  @param source The instance of the PSE membership service which
144      *  created this authenticator.
145      *  @param application Anything entered into the identity info section of
146      *  the Authentication credential is ignored.
147      **/
148     EngineAuthenticator(PSEMembershipService source, AuthenticationCredential application, PSEAuthenticatorEngine authenticatorEngine) {
149         // this( source, application );
150         
151         this.source = source;
152         this.application = application;
153         this.seedCert = authenticatorEngine.getX509Certificate();
154         this.authenticatorEngine = authenticatorEngine;
155     }
156     
157     /**
158      * Creates an authenticator for the PSE membership service. Anything entered
159      * into the identity info section of the Authentication credential is
160      * ignored.
161      *
162      *  @param source The instance of the PSE membership service which
163      *  created this authenticator.
164      *  @param application Anything entered into the identity info section of
165      *  the Authentication credential is ignored.
166      **/
167     EngineAuthenticator(PSEMembershipService source, AuthenticationCredential application) {
168         this.source = source;
169         this.application = application;
170         
171         // XXX 20010328 bondolo@jxta.org Could do something with the authentication credential here.
172     }
173     
174     /**
175      * {@inheritDoc}
176      **/
177     @Override
178     protected void finalize() throws Throwable {
179         if (null != store_password) {
180             Arrays.fill(store_password, '\0');
181         }
182         
183         if (null != key_password) {
184             Arrays.fill(key_password, '\0');
185         }
186         
187         super.finalize();
188     }
189     
190     /**
191      * {@inheritDoc}
192      **/
193     public MembershipService getSourceService() {
194         return (MembershipService) source.getInterface();
195     }
196     
197     /**
198      * {@inheritDoc}
199      **/
200     public AuthenticationCredential getAuthenticationCredential() {
201         return application;
202     }
203     
204     /**
205      * {@inheritDoc}
206      **/
207     public String getMethodName() {
208         return "EngineAuthentication";
209     }
210     
211     /**
212      * {@inheritDoc}
213      **/
214     synchronized public boolean isReadyForJoin() {
215         if (null != seedCert) {
216             return authenticatorEngine.isEnginePresent();
217         } else {
218             return source.pseStore.validPasswd(identity, store_password, key_password);
219         }
220     }
221     
222     /**
223      *  Get KeyStore password
224      **/
225     public char[] getAuth1_KeyStorePassword() {
226         return store_password;
227     }
228     
229     /**
230      *  Set KeyStore password
231      **/
232     public void setAuth1_KeyStorePassword(String store_password) {
233         if (null == store_password) {
234             setAuth1_KeyStorePassword((char[]) null);
235         } else {
236             setAuth1_KeyStorePassword(store_password.toCharArray());
237         }
238     }
239     
240     /**
241      *  Set KeyStore password
242      **/
243     public void setAuth1_KeyStorePassword(char[] store_password) {
244         if (null != this.store_password) {
245             Arrays.fill(this.store_password, '\0');
246         }
247         
248         if (null == store_password) {
249             this.store_password = null;
250         } else {
251             this.store_password = store_password.clone();
252         }
253     }
254     
255     /**
256      *  Return the available identities.
257      **/
258     public PeerID[] getIdentities(char[] store_password) {
259         
260         if (seedCert != null) {
261             PeerID[] seed = { source.group.getPeerID() };
262
263             return seed;
264         } else {
265             try {
266                 ID[] allkeys = source.pseStore.getKeysList(store_password);
267                 
268                 // XXX bondolo 20040329 it may be appropriate to login
269                 // something other than a peer id.
270                 List peersOnly = new ArrayList();
271                 
272                 Iterator eachKey = Arrays.asList(allkeys).iterator();
273                 
274                 while (eachKey.hasNext()) {
275                     ID aKey = (ID) eachKey.next();
276                     
277                     if (aKey instanceof PeerID) {
278                         peersOnly.add(aKey);
279                     }
280                 }
281                 
282                 return (PeerID[]) peersOnly.toArray(new PeerID[peersOnly.size()]);
283             } catch (IOException failed) {
284                 return null;
285             } catch (KeyStoreException failed) {
286                 return null;
287             }
288         }
289     }
290     
291     public X509Certificate getCertificate(char[] store_password, ID aPeer) {
292         if (seedCert != null) {
293             if (aPeer.equals(source.group.getPeerID())) {
294                 return seedCert;
295             } else {
296                 return null;
297             }
298         } else {
299             try {
300                 return source.pseStore.getTrustedCertificate(aPeer, store_password);
301             } catch (IOException failed) {
302                 return null;
303             } catch (KeyStoreException failed) {
304                 return null;
305             }
306         }
307     }
308     
309     /**
310      *  Get Identity
311      **/
312     public ID getAuth2Identity() {
313         return identity;
314     }
315     
316     /**
317      *  Set Identity
318      **/
319     public void setAuth2Identity(String id) {
320         try {
321             URI idURI = new URI(id);
322             ID identity = IDFactory.fromURI(idURI);
323
324             setAuth2Identity(identity);
325         } catch (URISyntaxException badID) {
326             throw new IllegalArgumentException("Bad ID");
327         } 
328     }
329     
330     /**
331      *  Set Identity
332      **/
333     public void setAuth2Identity(ID identity) {
334         this.identity = identity;
335     }
336     
337     /**
338      *  Get identity password
339      **/
340     public char[] getAuth3_IdentityPassword() {
341         return key_password;
342     }
343     
344     /**
345      *  Set identity password
346      **/
347     public void setAuth3_IdentityPassword(String key_password) {
348         setAuth3_IdentityPassword(key_password.toCharArray());
349     }
350     
351     /**
352      *  Set identity password
353      **/
354     public void setAuth3_IdentityPassword(char[] key_password) {
355         if (null != this.key_password) {
356             Arrays.fill(this.key_password, '\0');
357         }
358         
359         if (null == key_password) {
360             this.key_password = null;
361         } else {
362             this.key_password = key_password.clone();
363         }
364     }
365 }