org.jcp.xml.dsig.internal.dom.DOMX509Data Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal.dom;
/* */
/* */ import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
/* */ import com.sun.org.apache.xml.internal.security.utils.Base64;
/* */ import java.io.ByteArrayInputStream;
/* */ import java.security.cert.CRLException;
/* */ import java.security.cert.CertificateEncodingException;
/* */ import java.security.cert.CertificateException;
/* */ import java.security.cert.CertificateFactory;
/* */ import java.security.cert.X509CRL;
/* */ import java.security.cert.X509Certificate;
/* */ import java.util.ArrayList;
/* */ import java.util.Arrays;
/* */ import java.util.Collections;
/* */ import java.util.List;
/* */ import javax.security.auth.x500.X500Principal;
/* */ import javax.xml.crypto.MarshalException;
/* */ import javax.xml.crypto.XMLStructure;
/* */ import javax.xml.crypto.dom.DOMCryptoContext;
/* */ import javax.xml.crypto.dsig.keyinfo.X509Data;
/* */ import javax.xml.crypto.dsig.keyinfo.X509IssuerSerial;
/* */ import org.w3c.dom.Document;
/* */ import org.w3c.dom.Element;
/* */ import org.w3c.dom.Node;
/* */ import org.w3c.dom.NodeList;
/* */
/* */ public final class DOMX509Data extends DOMStructure
/* */ implements X509Data
/* */ {
/* */ private final List content;
/* */ private CertificateFactory cf;
/* */
/* */ public DOMX509Data(List content)
/* */ {
/* 53 */ if (content == null) {
/* 54 */ throw new NullPointerException("content cannot be null");
/* */ }
/* 56 */ List contentCopy = new ArrayList(content);
/* 57 */ if (contentCopy.isEmpty()) {
/* 58 */ throw new IllegalArgumentException("content cannot be empty");
/* */ }
/* 60 */ int i = 0; for (int size = contentCopy.size(); i < size; i++) {
/* 61 */ Object x509Type = contentCopy.get(i);
/* 62 */ if ((x509Type instanceof String))
/* 63 */ new X500Principal((String)x509Type);
/* 64 */ else if ((!(x509Type instanceof byte[])) && (!(x509Type instanceof X509Certificate)) && (!(x509Type instanceof X509CRL)) && (!(x509Type instanceof XMLStructure)))
/* */ {
/* 68 */ throw new ClassCastException("content[" + i + "] is not a valid X509Data type");
/* */ }
/* */ }
/* */
/* 72 */ this.content = Collections.unmodifiableList(contentCopy);
/* */ }
/* */
/* */ public DOMX509Data(Element xdElem)
/* */ throws MarshalException
/* */ {
/* 83 */ NodeList nl = xdElem.getChildNodes();
/* 84 */ int length = nl.getLength();
/* 85 */ List content = new ArrayList(length);
/* 86 */ for (int i = 0; i < length; i++) {
/* 87 */ Node child = nl.item(i);
/* */
/* 89 */ if (child.getNodeType() == 1)
/* */ {
/* 93 */ Element childElem = (Element)child;
/* 94 */ String localName = childElem.getLocalName();
/* 95 */ if (localName.equals("X509Certificate"))
/* 96 */ content.add(unmarshalX509Certificate(childElem));
/* 97 */ else if (localName.equals("X509IssuerSerial"))
/* 98 */ content.add(new DOMX509IssuerSerial(childElem));
/* 99 */ else if (localName.equals("X509SubjectName"))
/* 100 */ content.add(childElem.getFirstChild().getNodeValue());
/* 101 */ else if (localName.equals("X509SKI"))
/* */ try {
/* 103 */ content.add(Base64.decode(childElem));
/* */ } catch (Base64DecodingException bde) {
/* 105 */ throw new MarshalException("cannot decode X509SKI", bde);
/* */ }
/* 107 */ else if (localName.equals("X509CRL"))
/* 108 */ content.add(unmarshalX509CRL(childElem));
/* */ else
/* 110 */ content.add(new javax.xml.crypto.dom.DOMStructure(childElem));
/* */ }
/* */ }
/* 113 */ this.content = Collections.unmodifiableList(content);
/* */ }
/* */
/* */ public List getContent() {
/* 117 */ return this.content;
/* */ }
/* */
/* */ public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) throws MarshalException
/* */ {
/* 122 */ Document ownerDoc = DOMUtils.getOwnerDocument(parent);
/* */
/* 124 */ Element xdElem = DOMUtils.createElement(ownerDoc, "X509Data", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/* */
/* 128 */ int i = 0; for (int size = this.content.size(); i < size; i++) {
/* 129 */ Object object = this.content.get(i);
/* 130 */ if ((object instanceof X509Certificate))
/* 131 */ marshalCert((X509Certificate)object, xdElem, ownerDoc, dsPrefix);
/* 132 */ else if ((object instanceof XMLStructure)) {
/* 133 */ if ((object instanceof X509IssuerSerial)) {
/* 134 */ ((DOMX509IssuerSerial)object).marshal(xdElem, dsPrefix, context);
/* */ }
/* */ else {
/* 137 */ javax.xml.crypto.dom.DOMStructure domContent = (javax.xml.crypto.dom.DOMStructure)object;
/* */
/* 139 */ DOMUtils.appendChild(xdElem, domContent.getNode());
/* */ }
/* 141 */ } else if ((object instanceof byte[]))
/* 142 */ marshalSKI((byte[])object, xdElem, ownerDoc, dsPrefix);
/* 143 */ else if ((object instanceof String))
/* 144 */ marshalSubjectName((String)object, xdElem, ownerDoc, dsPrefix);
/* 145 */ else if ((object instanceof X509CRL)) {
/* 146 */ marshalCRL((X509CRL)object, xdElem, ownerDoc, dsPrefix);
/* */ }
/* */ }
/* */
/* 150 */ parent.appendChild(xdElem);
/* */ }
/* */
/* */ private void marshalSKI(byte[] skid, Node parent, Document doc, String dsPrefix)
/* */ {
/* 156 */ Element skidElem = DOMUtils.createElement(doc, "X509SKI", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/* */
/* 158 */ skidElem.appendChild(doc.createTextNode(Base64.encode(skid)));
/* 159 */ parent.appendChild(skidElem);
/* */ }
/* */
/* */ private void marshalSubjectName(String name, Node parent, Document doc, String dsPrefix)
/* */ {
/* 165 */ Element snElem = DOMUtils.createElement(doc, "X509SubjectName", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/* */
/* 167 */ snElem.appendChild(doc.createTextNode(name));
/* 168 */ parent.appendChild(snElem);
/* */ }
/* */
/* */ private void marshalCert(X509Certificate cert, Node parent, Document doc, String dsPrefix)
/* */ throws MarshalException
/* */ {
/* 174 */ Element certElem = DOMUtils.createElement(doc, "X509Certificate", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/* */ try
/* */ {
/* 177 */ certElem.appendChild(doc.createTextNode(Base64.encode(cert.getEncoded())));
/* */ }
/* */ catch (CertificateEncodingException e) {
/* 180 */ throw new MarshalException("Error encoding X509Certificate", e);
/* */ }
/* 182 */ parent.appendChild(certElem);
/* */ }
/* */
/* */ private void marshalCRL(X509CRL crl, Node parent, Document doc, String dsPrefix)
/* */ throws MarshalException
/* */ {
/* 188 */ Element crlElem = DOMUtils.createElement(doc, "X509CRL", "http://www.w3.org/2000/09/xmldsig#", dsPrefix);
/* */ try
/* */ {
/* 191 */ crlElem.appendChild(doc.createTextNode(Base64.encode(crl.getEncoded())));
/* */ }
/* */ catch (CRLException e) {
/* 194 */ throw new MarshalException("Error encoding X509CRL", e);
/* */ }
/* 196 */ parent.appendChild(crlElem);
/* */ }
/* */
/* */ private X509Certificate unmarshalX509Certificate(Element elem) throws MarshalException
/* */ {
/* */ try {
/* 202 */ ByteArrayInputStream bs = unmarshalBase64Binary(elem);
/* 203 */ return (X509Certificate)this.cf.generateCertificate(bs);
/* */ } catch (CertificateException e) {
/* 205 */ throw new MarshalException("Cannot create X509Certificate", e);
/* */ }
/* */ }
/* */
/* */ private X509CRL unmarshalX509CRL(Element elem) throws MarshalException {
/* */ try {
/* 211 */ ByteArrayInputStream bs = unmarshalBase64Binary(elem);
/* 212 */ return (X509CRL)this.cf.generateCRL(bs);
/* */ } catch (CRLException e) {
/* 214 */ throw new MarshalException("Cannot create X509CRL", e);
/* */ }
/* */ }
/* */
/* */ private ByteArrayInputStream unmarshalBase64Binary(Element elem) throws MarshalException
/* */ {
/* */ try {
/* 221 */ if (this.cf == null) {
/* 222 */ this.cf = CertificateFactory.getInstance("X.509");
/* */ }
/* 224 */ return new ByteArrayInputStream(Base64.decode(elem));
/* */ } catch (CertificateException e) {
/* 226 */ throw new MarshalException("Cannot create CertificateFactory", e);
/* */ } catch (Base64DecodingException bde) {
/* 228 */ throw new MarshalException("Cannot decode Base64-encoded val", bde);
/* */ }
/* */ }
/* */
/* */ public boolean equals(Object o) {
/* 233 */ if (this == o) {
/* 234 */ return true;
/* */ }
/* */
/* 237 */ if (!(o instanceof X509Data)) {
/* 238 */ return false;
/* */ }
/* 240 */ X509Data oxd = (X509Data)o;
/* */
/* 242 */ List ocontent = oxd.getContent();
/* 243 */ int size = this.content.size();
/* 244 */ if (size != ocontent.size()) {
/* 245 */ return false;
/* */ }
/* */
/* 248 */ for (int i = 0; i < size; i++) {
/* 249 */ Object x = this.content.get(i);
/* 250 */ Object ox = ocontent.get(i);
/* 251 */ if ((x instanceof byte[])) {
/* 252 */ if ((!(ox instanceof byte[])) || (!Arrays.equals((byte[])x, (byte[])ox)))
/* */ {
/* 254 */ return false;
/* */ }
/* */ }
/* 257 */ else if (!x.equals(ox)) {
/* 258 */ return false;
/* */ }
/* */
/* */ }
/* */
/* 263 */ return true;
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.dom.DOMX509Data
* JD-Core Version: 0.6.2
*/