1 17 package com.sun.org.apache.xml.internal.security.keys.content.x509; 18 19 20 21 import java.io.ByteArrayInputStream ; 22 import java.security.PublicKey ; 23 import java.security.cert.CertificateException ; 24 import java.security.cert.CertificateFactory ; 25 import java.security.cert.X509Certificate ; 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 ; 31 import org.w3c.dom.Element ; 32 33 34 38 public class XMLX509Certificate extends SignatureElementProxy 39 implements XMLX509DataContent { 40 41 42 static java.util.logging.Logger log = 43 java.util.logging.Logger.getLogger(XMLX509Certificate.class.getName()); 44 45 46 public static final String JCA_CERT_ID = "X.509"; 47 48 55 public XMLX509Certificate(Element element, String BaseURI) 56 throws XMLSecurityException { 57 super(element, BaseURI); 58 } 59 60 66 public XMLX509Certificate(Document doc, byte[] certificateBytes) { 67 68 super(doc); 69 70 this.addBase64Text(certificateBytes); 71 } 72 73 80 public XMLX509Certificate(Document doc, X509Certificate x509certificate) 81 throws XMLSecurityException { 82 83 super(doc); 84 85 try { 86 this.addBase64Text(x509certificate.getEncoded()); 87 } catch (java.security.cert.CertificateEncodingException ex) { 88 throw new XMLSecurityException("empty", ex); 89 } 90 } 91 92 98 public byte[] getCertificateBytes() throws XMLSecurityException { 99 return this.getBytesFromTextChild(); 100 } 101 102 108 public X509Certificate getX509Certificate() throws XMLSecurityException { 109 110 try { 111 byte certbytes[] = this.getCertificateBytes(); 112 CertificateFactory certFact = 113 CertificateFactory.getInstance(XMLX509Certificate.JCA_CERT_ID); 114 X509Certificate cert = 115 (X509Certificate ) certFact 116 .generateCertificate(new ByteArrayInputStream (certbytes)); 117 118 if (cert != null) { 119 return cert; 120 } 121 122 return null; 123 } catch (CertificateException ex) { 124 throw new XMLSecurityException("empty", ex); 125 } 126 } 127 128 134 public PublicKey getPublicKey() throws XMLSecurityException { 135 136 X509Certificate cert = this.getX509Certificate(); 137 138 if (cert != null) { 139 return cert.getPublicKey(); 140 } 141 142 return null; 143 } 144 145 146 public boolean equals(Object 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 156 return java.security.MessageDigest.isEqual(other.getCertificateBytes(), 157 this.getCertificateBytes()); 158 } catch (XMLSecurityException ex) { 159 return false; 160 } 161 } 162 163 164 public String getBaseLocalName() { 165 return Constants._TAG_X509CERTIFICATE; 166 } 167 } 168 | Popular Tags |