KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > content > x509 > XMLX509Certificate


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.keys.content.x509;
18
19
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.security.PublicKey JavaDoc;
23 import java.security.cert.CertificateException JavaDoc;
24 import java.security.cert.CertificateFactory JavaDoc;
25 import java.security.cert.X509Certificate JavaDoc;
26
27 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
28 import com.sun.org.apache.xml.internal.security.utils.Constants;
29 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33
34 /**
35  *
36  * @author $Author: raul $
37  */

38 public class XMLX509Certificate extends SignatureElementProxy
39         implements XMLX509DataContent {
40
41    /** {@link java.util.logging} logging facility */
42     static java.util.logging.Logger JavaDoc log =
43         java.util.logging.Logger.getLogger(XMLX509Certificate.class.getName());
44
45    /** Field JCA_CERT_ID */
46    public static final String JavaDoc JCA_CERT_ID = "X.509";
47
48    /**
49     * Constructor X509Certificate
50     *
51     * @param element
52     * @param BaseURI
53     * @throws XMLSecurityException
54     */

55    public XMLX509Certificate(Element JavaDoc element, String JavaDoc BaseURI)
56            throws XMLSecurityException {
57       super(element, BaseURI);
58    }
59
60    /**
61     * Constructor X509Certificate
62     *
63     * @param doc
64     * @param certificateBytes
65     */

66    public XMLX509Certificate(Document JavaDoc doc, byte[] certificateBytes) {
67
68       super(doc);
69
70       this.addBase64Text(certificateBytes);
71    }
72
73    /**
74     * Constructor XMLX509Certificate
75     *
76     * @param doc
77     * @param x509certificate
78     * @throws XMLSecurityException
79     */

80    public XMLX509Certificate(Document JavaDoc doc, X509Certificate JavaDoc x509certificate)
81            throws XMLSecurityException {
82
83       super(doc);
84
85       try {
86          this.addBase64Text(x509certificate.getEncoded());
87       } catch (java.security.cert.CertificateEncodingException JavaDoc ex) {
88          throw new XMLSecurityException("empty", ex);
89       }
90    }
91
92    /**
93     * Method getCertificateBytes
94     *
95     * @return the certificate bytes
96     * @throws XMLSecurityException
97     */

98    public byte[] getCertificateBytes() throws XMLSecurityException {
99       return this.getBytesFromTextChild();
100    }
101
102    /**
103     * Method getX509Certificate
104     *
105     * @return the x509 certificate
106     * @throws XMLSecurityException
107     */

108    public X509Certificate JavaDoc getX509Certificate() throws XMLSecurityException {
109
110       try {
111          byte certbytes[] = this.getCertificateBytes();
112          CertificateFactory JavaDoc certFact =
113             CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
114          X509Certificate JavaDoc cert =
115             (X509Certificate JavaDoc) certFact
116                .generateCertificate(new ByteArrayInputStream JavaDoc(certbytes));
117
118          if (cert != null) {
119             return cert;
120          }
121
122          return null;
123       } catch (CertificateException JavaDoc ex) {
124          throw new XMLSecurityException("empty", ex);
125       }
126    }
127
128    /**
129     * Method getPublicKey
130     *
131     * @return teh publickey
132     * @throws XMLSecurityException
133     */

134    public PublicKey JavaDoc getPublicKey() throws XMLSecurityException {
135
136       X509Certificate JavaDoc cert = this.getX509Certificate();
137
138       if (cert != null) {
139          return cert.getPublicKey();
140       }
141
142       return null;
143    }
144
145    /** @inheritDoc */
146    public boolean equals(Object JavaDoc obj) {
147
148       try {
149          if (!obj.getClass().getName().equals(this.getClass().getName())) {
150             return false;
151          }
152
153          XMLX509Certificate other = (XMLX509Certificate) obj;
154
155          /** $todo$ or should be create X509Certificates and use the equals() from the Certs */
156          return java.security.MessageDigest.isEqual(other.getCertificateBytes(),
157                                         this.getCertificateBytes());
158       } catch (XMLSecurityException ex) {
159          return false;
160       }
161    }
162
163    /** @inheritDoc */
164    public String JavaDoc getBaseLocalName() {
165       return Constants._TAG_X509CERTIFICATE;
166    }
167 }
168
Popular Tags