blob: 51f81e529015f5e7855502cce097970d067c36a7 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Copyright 1999-2004 The Apache Software Foundation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21package com.sun.org.apache.xml.internal.security.keys.content.x509;
22
23
24
25import java.io.ByteArrayInputStream;
26import java.security.PublicKey;
27import java.security.cert.CertificateException;
28import java.security.cert.CertificateFactory;
29import java.security.cert.X509Certificate;
30
31import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
32import com.sun.org.apache.xml.internal.security.utils.Constants;
33import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
34import org.w3c.dom.Document;
35import org.w3c.dom.Element;
36
37
38/**
39 *
40 * @author $Author: raul $
41 */
42public class XMLX509Certificate extends SignatureElementProxy
43 implements XMLX509DataContent {
44
45 /** {@link java.util.logging} logging facility */
46 static java.util.logging.Logger log =
47 java.util.logging.Logger.getLogger(XMLX509Certificate.class.getName());
48
49 /** Field JCA_CERT_ID */
50 public static final String JCA_CERT_ID = "X.509";
51
52 /**
53 * Constructor X509Certificate
54 *
55 * @param element
56 * @param BaseURI
57 * @throws XMLSecurityException
58 */
59 public XMLX509Certificate(Element element, String BaseURI)
60 throws XMLSecurityException {
61 super(element, BaseURI);
62 }
63
64 /**
65 * Constructor X509Certificate
66 *
67 * @param doc
68 * @param certificateBytes
69 */
70 public XMLX509Certificate(Document doc, byte[] certificateBytes) {
71
72 super(doc);
73
74 this.addBase64Text(certificateBytes);
75 }
76
77 /**
78 * Constructor XMLX509Certificate
79 *
80 * @param doc
81 * @param x509certificate
82 * @throws XMLSecurityException
83 */
84 public XMLX509Certificate(Document doc, X509Certificate x509certificate)
85 throws XMLSecurityException {
86
87 super(doc);
88
89 try {
90 this.addBase64Text(x509certificate.getEncoded());
91 } catch (java.security.cert.CertificateEncodingException ex) {
92 throw new XMLSecurityException("empty", ex);
93 }
94 }
95
96 /**
97 * Method getCertificateBytes
98 *
99 * @return the certificate bytes
100 * @throws XMLSecurityException
101 */
102 public byte[] getCertificateBytes() throws XMLSecurityException {
103 return this.getBytesFromTextChild();
104 }
105
106 /**
107 * Method getX509Certificate
108 *
109 * @return the x509 certificate
110 * @throws XMLSecurityException
111 */
112 public X509Certificate getX509Certificate() throws XMLSecurityException {
113
114 try {
115 byte certbytes[] = this.getCertificateBytes();
116 CertificateFactory certFact =
117 CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID);
118 X509Certificate cert =
119 (X509Certificate) certFact
120 .generateCertificate(new ByteArrayInputStream(certbytes));
121
122 if (cert != null) {
123 return cert;
124 }
125
126 return null;
127 } catch (CertificateException ex) {
128 throw new XMLSecurityException("empty", ex);
129 }
130 }
131
132 /**
133 * Method getPublicKey
134 *
135 * @return teh publickey
136 * @throws XMLSecurityException
137 */
138 public PublicKey getPublicKey() throws XMLSecurityException {
139
140 X509Certificate cert = this.getX509Certificate();
141
142 if (cert != null) {
143 return cert.getPublicKey();
144 }
145
146 return null;
147 }
148
149 /** @inheritDoc */
150 public boolean equals(Object obj) {
151
152 try {
153 if (!obj.getClass().getName().equals(this.getClass().getName())) {
154 return false;
155 }
156
157 XMLX509Certificate other = (XMLX509Certificate) obj;
158
159 /** $todo$ or should be create X509Certificates and use the equals() from the Certs */
160 return java.security.MessageDigest.isEqual(other.getCertificateBytes(),
161 this.getCertificateBytes());
162 } catch (XMLSecurityException ex) {
163 return false;
164 }
165 }
166
167 /** @inheritDoc */
168 public String getBaseLocalName() {
169 return Constants._TAG_X509CERTIFICATE;
170 }
171}