Path: blob/master/test/jdk/javax/xml/crypto/dsig/ErrorHandlerPermissions.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.ByteArrayInputStream;26import java.io.File;27import java.security.KeyFactory;28import java.security.PublicKey;29import java.security.spec.X509EncodedKeySpec;30import java.util.Base64;31import javax.xml.XMLConstants;32import javax.xml.crypto.Data;33import javax.xml.crypto.KeySelector;34import javax.xml.crypto.OctetStreamData;35import javax.xml.crypto.URIDereferencer;36import javax.xml.crypto.URIReference;37import javax.xml.crypto.URIReferenceException;38import javax.xml.crypto.XMLCryptoContext;39import javax.xml.crypto.dsig.XMLSignature;40import javax.xml.crypto.dsig.XMLSignatureFactory;41import javax.xml.crypto.dsig.dom.DOMValidateContext;42import javax.xml.parsers.DocumentBuilderFactory;43import org.w3c.dom.Document;44import org.w3c.dom.Element;45import org.w3c.dom.NodeList;4647/**48* @test49* @bug 807914050* @summary Check if IgnoreAllErrorHandler doesn't require additional permission51* @run main/othervm/java.security.policy=ErrorHandlerPermissions.policy52* ErrorHandlerPermissions53*/54public class ErrorHandlerPermissions {5556private final static String FS = System.getProperty("file.separator");57private final static String DIR = System.getProperty("test.src", ".");58private final static String DATA_DIR = DIR + FS + "data";59private final static String SIGNATURE = DATA_DIR + FS +60"signature-external-rsa.xml";6162private static final String validationKey =63"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnx4TdvPSA5vcsPi0OJZi9Ox0Z" +64"2FRz2oeUCtuWoyEg0kUCeFd+jJZMstDJUiZNSOeuCO3FWSpdJgAwI4zlveHvuU/o" +65"qHSa1eYTObOCvxfVYGGflWsSvGXyiANtRWVUrYODBeyL+2pWxDYh+Fi5EKizPfTG" +66"wRjBVRSkRZKTnSjnQwIDAQAB";6768private static final URIDereferencer dereferencer =69new DummyURIDereferencer();7071public static void main(String[] args) throws Exception {72DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();73dbf.setNamespaceAware(true);74dbf.setValidating(false);75dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);76Document doc = dbf.newDocumentBuilder().parse(new File(SIGNATURE));77NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS,78"Signature");79if (nl.getLength() == 0) {80throw new RuntimeException("Couldn't find 'Signature' element");81}82Element element = (Element) nl.item(0);8384byte[] keyBytes = Base64.getDecoder().decode(validationKey);85X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);86KeyFactory kf = KeyFactory.getInstance("RSA");87PublicKey key = kf.generatePublic(spec);88KeySelector ks = KeySelector.singletonKeySelector(key);8990DOMValidateContext vc = new DOMValidateContext(ks, element);9192// disable secure validation mode93vc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE);9495// set a dummy dereferencer to be able to get content by references96vc.setURIDereferencer(dereferencer);9798XMLSignatureFactory factory = XMLSignatureFactory.getInstance();99XMLSignature signature = factory.unmarshalXMLSignature(vc);100101// run validation102signature.validate(vc);103}104105/**106* This URIDereferencer returns a static XML document.107*/108private static class DummyURIDereferencer implements URIDereferencer {109110@Override111public Data dereference(final URIReference ref, XMLCryptoContext ctx)112throws URIReferenceException {113// return static content114return new OctetStreamData(new ByteArrayInputStream(115"<test>test</test>".getBytes()), ref.getURI(),116ref.getType());117}118}119120}121122123