KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > security > JBoss14Crypto


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  *
7  * Created on Jan 20, 2004
8  */

9 package org.jboss.net.axis.security;
10
11 import java.security.KeyStore JavaDoc;
12 import java.security.cert.Certificate JavaDoc;
13 import java.security.cert.X509Certificate JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import javax.security.auth.x500.X500Principal JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import org.apache.ws.security.components.crypto.Merlin;
21
22 /**
23  * <dl>
24  * <dt><b>Title: </b><dd>JBoss14Crypto</dd>
25  * <p>
26  * <dt><b>Description: </b><dd>Crypto implementation that can be instantiated with a given keystore (presumably
27  * obtained from the SecurityDomain) rather than the properties file expected by Merlin.
28  * </dd>
29  * <p>
30  * </dl>
31  * @author <a HREF="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
32  * @version $Revision: 1.3 $
33  */

34 public class JBoss14Crypto extends Merlin implements JBossCrypto
35 {
36    private Logger log = Logger.getLogger(getClass());
37    //private KeyStore keystore = null;
38

39    public JBoss14Crypto(KeyStore JavaDoc keystore) throws Exception JavaDoc
40    {
41       super(null);
42       
43       //bah, certpath validation requires properties . . .
44
this.properties = new Properties JavaDoc();
45
46       if (keystore != null)
47       {
48          if (log.isDebugEnabled())
49             log.debug("Creating new JBoss14Crypto using a " + keystore.getType() + " keystore.");
50          setKeyStore(keystore);
51          //better save a copy for ourselves as well
52
//this.keystore = keystore;
53
}
54       else
55       {
56          if (log.isDebugEnabled())
57             log.debug("Creating new JBoss14Crypto WITHOUT a keystore.");
58       }
59    }
60
61    public String JavaDoc getAliasForX500Principal(X500Principal JavaDoc dn) throws Exception JavaDoc
62    {
63       String JavaDoc alias = null;
64       foundcert : for (Enumeration JavaDoc enu = keystore.aliases(); enu.hasMoreElements();)
65       {
66          String JavaDoc element = (String JavaDoc) enu.nextElement();
67
68          //if (!keystore.isCertificateEntry(element))
69
// continue;
70

71          Certificate JavaDoc[] certs = keystore.getCertificateChain(element);
72          if (certs == null)
73          {
74             Certificate JavaDoc cert = keystore.getCertificate(element);
75             if (cert != null)
76                certs = new Certificate JavaDoc[]{cert};
77          }
78          if (certs != null)
79          {
80             for (int i = 0; i < certs.length; i++)
81             {
82                if (!(certs[i] instanceof X509Certificate JavaDoc))
83                   continue;
84
85                X509Certificate JavaDoc x509cert = (X509Certificate JavaDoc) certs[i];
86                if (dn.equals(x509cert.getSubjectX500Principal()))
87                {
88                   alias = element;
89                   break foundcert;
90                }
91             }
92          }
93       }
94       return alias;
95    }
96 }
97
Popular Tags