From a3ba60010bfde37ad6eb4bdd3ed79d260178ac73 Mon Sep 17 00:00:00 2001 From: asmotrak Date: Mon, 18 May 2015 19:26:48 +0300 Subject: [PATCH] 8079140: IgnoreAllErrorHandler should use doPrivileged when it reads system properties Reviewed-by: mullan --- .../security/utils/IgnoreAllErrorHandler.java | 23 +++- .../crypto/dsig/ErrorHandlerPermissions.java | 121 ++++++++++++++++++ .../dsig/ErrorHandlerPermissions.policy | 5 + .../dsig/data/signature-external-rsa.xml | 3 + 4 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 test/javax/xml/crypto/dsig/ErrorHandlerPermissions.java create mode 100644 test/javax/xml/crypto/dsig/ErrorHandlerPermissions.policy create mode 100644 test/javax/xml/crypto/dsig/data/signature-external-rsa.xml diff --git a/src/share/classes/com/sun/org/apache/xml/internal/security/utils/IgnoreAllErrorHandler.java b/src/share/classes/com/sun/org/apache/xml/internal/security/utils/IgnoreAllErrorHandler.java index d06a41ffd..4d0ab5266 100644 --- a/src/share/classes/com/sun/org/apache/xml/internal/security/utils/IgnoreAllErrorHandler.java +++ b/src/share/classes/com/sun/org/apache/xml/internal/security/utils/IgnoreAllErrorHandler.java @@ -35,19 +35,30 @@ import org.xml.sax.SAXParseException; public class IgnoreAllErrorHandler implements ErrorHandler { /** {@link org.apache.commons.logging} logging facility */ - private static java.util.logging.Logger log = + private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(IgnoreAllErrorHandler.class.getName()); /** Field throwExceptions */ - private static final boolean warnOnExceptions = - System.getProperty("com.sun.org.apache.xml.internal.security.test.warn.on.exceptions", "false").equals("true"); + private static final boolean warnOnExceptions = getProperty( + "com.sun.org.apache.xml.internal.security.test.warn.on.exceptions"); /** Field throwExceptions */ - private static final boolean throwExceptions = - System.getProperty("com.sun.org.apache.xml.internal.security.test.throw.exceptions", "false").equals("true"); + private static final boolean throwExceptions = getProperty( + "com.sun.org.apache.xml.internal.security.test.throw.exceptions"); + private static boolean getProperty(String name) { + return java.security.AccessController.doPrivileged( + new java.security.PrivilegedAction() { + + @Override + public Boolean run() { + return Boolean.getBoolean(name); + } + }); + } /** @inheritDoc */ + @Override public void warning(SAXParseException ex) throws SAXException { if (IgnoreAllErrorHandler.warnOnExceptions) { log.log(java.util.logging.Level.WARNING, "", ex); @@ -59,6 +70,7 @@ public class IgnoreAllErrorHandler implements ErrorHandler { /** @inheritDoc */ + @Override public void error(SAXParseException ex) throws SAXException { if (IgnoreAllErrorHandler.warnOnExceptions) { log.log(java.util.logging.Level.SEVERE, "", ex); @@ -70,6 +82,7 @@ public class IgnoreAllErrorHandler implements ErrorHandler { /** @inheritDoc */ + @Override public void fatalError(SAXParseException ex) throws SAXException { if (IgnoreAllErrorHandler.warnOnExceptions) { log.log(java.util.logging.Level.WARNING, "", ex); diff --git a/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.java b/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.java new file mode 100644 index 000000000..1231d3cfe --- /dev/null +++ b/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.security.KeyFactory; +import java.security.PublicKey; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; +import javax.xml.XMLConstants; +import javax.xml.crypto.Data; +import javax.xml.crypto.KeySelector; +import javax.xml.crypto.OctetStreamData; +import javax.xml.crypto.URIDereferencer; +import javax.xml.crypto.URIReference; +import javax.xml.crypto.URIReferenceException; +import javax.xml.crypto.XMLCryptoContext; +import javax.xml.crypto.dsig.XMLSignature; +import javax.xml.crypto.dsig.XMLSignatureFactory; +import javax.xml.crypto.dsig.dom.DOMValidateContext; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +/** + * @test + * @bug 8079140 + * @summary Check if IgnoreAllErrorHandler doesn't require additional permission + * @run main/othervm/java.security.policy=ErrorHandlerPermissions.policy + * ErrorHandlerPermissions + */ +public class ErrorHandlerPermissions { + + private final static String FS = System.getProperty("file.separator"); + private final static String DIR = System.getProperty("test.src", "."); + private final static String DATA_DIR = DIR + FS + "data"; + private final static String SIGNATURE = DATA_DIR + FS + + "signature-external-rsa.xml"; + + private static final String validationKey = + "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnx4TdvPSA5vcsPi0OJZi9Ox0Z" + + "2FRz2oeUCtuWoyEg0kUCeFd+jJZMstDJUiZNSOeuCO3FWSpdJgAwI4zlveHvuU/o" + + "qHSa1eYTObOCvxfVYGGflWsSvGXyiANtRWVUrYODBeyL+2pWxDYh+Fi5EKizPfTG" + + "wRjBVRSkRZKTnSjnQwIDAQAB"; + + private static final URIDereferencer dereferencer = + new DummyURIDereferencer(); + + public static void main(String[] args) throws Exception { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + dbf.setValidating(false); + dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); + Document doc = dbf.newDocumentBuilder().parse(new File(SIGNATURE)); + NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, + "Signature"); + if (nl.getLength() == 0) { + throw new RuntimeException("Couldn't find 'Signature' element"); + } + Element element = (Element) nl.item(0); + + byte[] keyBytes = Base64.getDecoder().decode(validationKey); + X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); + KeyFactory kf = KeyFactory.getInstance("RSA"); + PublicKey key = kf.generatePublic(spec); + KeySelector ks = KeySelector.singletonKeySelector(key); + + DOMValidateContext vc = new DOMValidateContext(ks, element); + + // disable secure validation mode + vc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE); + + // set a dummy dereferencer to be able to get content by references + vc.setURIDereferencer(dereferencer); + + XMLSignatureFactory factory = XMLSignatureFactory.getInstance(); + XMLSignature signature = factory.unmarshalXMLSignature(vc); + + // run validation + signature.validate(vc); + } + + /** + * This URIDereferencer returns a static XML document. + */ + private static class DummyURIDereferencer implements URIDereferencer { + + @Override + public Data dereference(final URIReference ref, XMLCryptoContext ctx) + throws URIReferenceException { + // return static content + return new OctetStreamData(new ByteArrayInputStream( + "test".getBytes()), ref.getURI(), + ref.getType()); + } + } + +} diff --git a/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.policy b/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.policy new file mode 100644 index 000000000..8db9c01c3 --- /dev/null +++ b/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.policy @@ -0,0 +1,5 @@ +grant { + permission java.util.PropertyPermission "test.src", "read"; + permission java.util.PropertyPermission "file.separator", "read"; + permission java.io.FilePermission "${test.src}/-", "read"; +}; diff --git a/test/javax/xml/crypto/dsig/data/signature-external-rsa.xml b/test/javax/xml/crypto/dsig/data/signature-external-rsa.xml new file mode 100644 index 000000000..fb3a0f4f9 --- /dev/null +++ b/test/javax/xml/crypto/dsig/data/signature-external-rsa.xml @@ -0,0 +1,3 @@ +test1Bq8FsjajUBYPD7stQeJSc66GlM=f6trDCcPsLLkIV/V4DGFbRf8b9Jwal8xGBDshNzEXwPmym2ChN85rbKIg/cbunf04F89/SXLo2v9 +AYjLcUr3G/Vz5YUmqNhnBvJukXgsIG0ddWl3mFi9Tk+CLINlbgfsaFqU9pQwFjmDyAqIrvZYqW7p +rTHLetv218mbUVBBAkc= \ No newline at end of file -- GitLab