diff -r 0f93a75b9213 -r 3810c9a2efa1 src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/keys/keyresolver/KeyResolverSpi.java --- a/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/keys/keyresolver/KeyResolverSpi.java Mon Jun 18 15:24:48 2018 -0700 +++ b/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/keys/keyresolver/KeyResolverSpi.java Tue Jun 19 08:06:35 2018 +0800 @@ -22,15 +22,23 @@ */ package com.sun.org.apache.xml.internal.security.keys.keyresolver; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.X509Certificate; import java.util.HashMap; import javax.crypto.SecretKey; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.ParserConfigurationException; import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; +import com.sun.org.apache.xml.internal.security.utils.XMLUtils; +import org.w3c.dom.Document; import org.w3c.dom.Element; +import org.xml.sax.SAXException; /** * This class is an abstract class for a child KeyInfo Element. @@ -45,7 +53,7 @@ public abstract class KeyResolverSpi { /** Field properties */ - protected java.util.Map properties = null; + protected java.util.Map properties; protected boolean globalResolver = false; @@ -84,7 +92,7 @@ Element element, String baseURI, StorageResolver storage ) throws KeyResolverException { throw new UnsupportedOperationException(); - }; + } /** * Method engineLookupAndResolvePublicKey @@ -107,19 +115,18 @@ } private KeyResolverSpi cloneIfNeeded() throws KeyResolverException { - KeyResolverSpi tmp = this; if (globalResolver) { try { @SuppressWarnings("deprecation") - KeyResolverSpi krs = getClass().newInstance(); - tmp = krs; + KeyResolverSpi tmp = getClass().newInstance(); + return tmp; } catch (InstantiationException e) { - throw new KeyResolverException("", e); + throw new KeyResolverException(e, ""); } catch (IllegalAccessException e) { - throw new KeyResolverException("", e); + throw new KeyResolverException(e, ""); } } - return tmp; + return this; } /** @@ -136,7 +143,7 @@ Element element, String baseURI, StorageResolver storage ) throws KeyResolverException{ throw new UnsupportedOperationException(); - }; + } /** * Method engineLookupResolveX509Certificate @@ -172,7 +179,7 @@ Element element, String baseURI, StorageResolver storage ) throws KeyResolverException{ throw new UnsupportedOperationException(); - }; + } /** * Method engineLookupAndResolveSecretKey @@ -223,7 +230,7 @@ */ public void engineSetProperty(String key, String value) { if (properties == null) { - properties = new HashMap(); + properties = new HashMap<>(); } properties.put(key, value); } @@ -260,4 +267,27 @@ this.globalResolver = globalResolver; } + + /** + * Parses a byte array and returns the parsed Element. + * + * @param bytes + * @return the Document Element after parsing bytes + * @throws KeyResolverException if something goes wrong + */ + protected static Element getDocFromBytes(byte[] bytes, boolean secureValidation) throws KeyResolverException { + DocumentBuilder db = null; + try (InputStream is = new ByteArrayInputStream(bytes)) { + db = XMLUtils.createDocumentBuilder(false, secureValidation); + Document doc = db.parse(is); + return doc.getDocumentElement(); + } catch (SAXException ex) { + throw new KeyResolverException(ex); + } catch (IOException ex) { + throw new KeyResolverException(ex); + } catch (ParserConfigurationException ex) { + throw new KeyResolverException(ex); + } + } + }