1 17 package com.sun.org.apache.xml.internal.security.keys.content.x509; 18 19 20 21 import java.io.IOException ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.InputStream ; 24 import java.security.cert.X509Certificate ; 25 import java.lang.reflect.Constructor ; 26 import java.lang.reflect.Method ; 27 28 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 29 import com.sun.org.apache.xml.internal.security.utils.Base64; 30 import com.sun.org.apache.xml.internal.security.utils.Constants; 31 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 35 import sun.security.util.DerValue; 36 37 38 44 public class XMLX509SKI extends SignatureElementProxy 45 implements XMLX509DataContent { 46 47 48 static java.util.logging.Logger log = 49 java.util.logging.Logger.getLogger(XMLX509SKI.class.getName()); 50 51 60 public static final String SKI_OID = "2.5.29.14"; 61 62 68 public XMLX509SKI(Document doc, byte[] skiBytes) { 69 70 super(doc); 71 72 this.addBase64Text(skiBytes); 73 } 74 75 82 public XMLX509SKI(Document doc, X509Certificate x509certificate) 83 throws XMLSecurityException { 84 85 super(doc); 86 87 this.addBase64Text(XMLX509SKI.getSKIBytesFromCert(x509certificate)); 88 } 89 90 97 public XMLX509SKI(Element element, String BaseURI) 98 throws XMLSecurityException { 99 super(element, BaseURI); 100 } 101 102 108 public byte[] getSKIBytes() throws XMLSecurityException { 109 return this.getBytesFromTextChild(); 110 } 111 112 121 public static byte[] getSKIBytesFromCert(X509Certificate cert) 122 throws XMLSecurityException { 123 124 try { 125 126 131 byte[] derEncodedValue = cert.getExtensionValue(XMLX509SKI.SKI_OID); 132 133 if (cert.getVersion() < 3) { 134 Object exArgs[] = { new Integer (cert.getVersion()) }; 135 136 throw new XMLSecurityException("certificate.noSki.lowVersion", 137 exArgs); 138 } 139 140 byte[] extensionValue = null; 141 142 145 try { 146 DerValue dervalue = new DerValue(derEncodedValue); 147 if (dervalue == null) { 148 throw new XMLSecurityException("certificate.noSki.null"); 149 } 150 if (dervalue.tag != DerValue.tag_OctetString) { 151 throw new XMLSecurityException("certificate.noSki.notOctetString"); 152 } 153 extensionValue = dervalue.getOctetString(); 154 } catch (NoClassDefFoundError e) { 155 } 156 157 160 if (extensionValue == null) { 161 try { 162 Class clazz = Class.forName("org.bouncycastle.asn1.DERInputStream"); 163 if (clazz != null) { 164 Constructor constructor = clazz.getConstructor(new Class []{InputStream .class}); 165 InputStream is = (InputStream ) constructor.newInstance(new Object []{new ByteArrayInputStream (derEncodedValue)}); 166 Method method = clazz.getMethod("readObject", new Class []{}); 167 Object obj = method.invoke(is, new Object []{}); 168 if (obj == null) { 169 throw new XMLSecurityException("certificate.noSki.null"); 170 } 171 Class clazz2 = Class.forName("org.bouncycastle.asn1.ASN1OctetString"); 172 if (!clazz2.isInstance(obj)) { 173 throw new XMLSecurityException("certificate.noSki.notOctetString"); 174 } 175 Method method2 = clazz2.getMethod("getOctets", new Class []{}); 176 extensionValue = (byte[]) method2.invoke(obj, new Object []{}); 177 } 178 } catch (Throwable t) { 179 } 180 } 181 182 185 byte abyte0[] = new byte[extensionValue.length - 2]; 186 187 System.arraycopy(extensionValue, 2, abyte0, 0, abyte0.length); 188 189 193 if (true) 194 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Base64 of SKI is " + Base64.encode(abyte0)); 195 196 return abyte0; 197 } catch (IOException ex) { 198 throw new XMLSecurityException("generic.EmptyMessage", ex); 199 } 200 } 201 202 203 public boolean equals(Object obj) { 204 205 if (!obj.getClass().getName().equals(this.getClass().getName())) { 206 return false; 207 } 208 209 XMLX509SKI other = (XMLX509SKI) obj; 210 211 try { 212 return java.security.MessageDigest.isEqual(other.getSKIBytes(), 213 this.getSKIBytes()); 214 } catch (XMLSecurityException ex) { 215 return false; 216 } 217 } 218 219 220 public String getBaseLocalName() { 221 return Constants._TAG_X509SKI; 222 } 223 } 224 | Popular Tags |