Path: blob/master/test/jdk/javax/xml/crypto/dsig/SecureValidation.java
41152 views
/*1* Copyright (c) 2021, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 824130626* @summary Tests for the jdk.xml.dsig.secureValidationPolicy security property27* on the RSASSA-PSS signature method28* @library /test/lib29* @modules java.base/sun.security.tools.keytool30* java.base/sun.security.x50931*/32import jdk.test.lib.Asserts;33import jdk.test.lib.security.XMLUtils;34import jdk.test.lib.Utils;35import org.w3c.dom.Document;36import org.w3c.dom.Element;37import sun.security.tools.keytool.CertAndKeyGen;38import sun.security.x509.X500Name;3940import javax.xml.crypto.MarshalException;41import javax.xml.crypto.dsig.DigestMethod;42import javax.xml.crypto.dsig.SignatureMethod;43import javax.xml.crypto.dsig.spec.RSAPSSParameterSpec;44import javax.xml.namespace.NamespaceContext;45import javax.xml.xpath.XPath;46import javax.xml.xpath.XPathConstants;47import javax.xml.xpath.XPathFactory;48import java.security.PrivateKey;49import java.security.cert.X509Certificate;50import java.security.spec.MGF1ParameterSpec;51import java.security.spec.PSSParameterSpec;52import java.util.Iterator;53import java.util.Objects;5455import static java.security.spec.PSSParameterSpec.TRAILER_FIELD_BC;5657public class SecureValidation {5859public static void main(String[] args) throws Exception {6061Document doc = XMLUtils.string2doc("<a><b>Text</b>Raw</a>");6263CertAndKeyGen g = new CertAndKeyGen("RSASSA-PSS", "RSASSA-PSS");64g.generate(2048);65X509Certificate cert = g.getSelfCertificate(new X500Name("CN=Me"), 100);66PrivateKey privateKey = g.getPrivateKey();67PSSParameterSpec pspec = new PSSParameterSpec("SHA-384", "MGF1",68MGF1ParameterSpec.SHA512, 48, TRAILER_FIELD_BC);6970// Sign with PSS with SHA-384 and SHA-51271Document signed = XMLUtils.signer(privateKey, cert)72.dm(DigestMethod.SHA384)73.sm(SignatureMethod.RSA_PSS, new RSAPSSParameterSpec(pspec))74.sign(doc);7576XPath xp = XPathFactory.newInstance().newXPath();77xp.setNamespaceContext(new NamespaceContext() {78@Override79public String getNamespaceURI(String prefix) {80return switch (prefix) {81case "ds" -> "http://www.w3.org/2000/09/xmldsig#";82case "pss" -> "http://www.w3.org/2007/05/xmldsig-more#";83default -> throw new IllegalArgumentException();84};85}8687@Override88public String getPrefix(String namespaceURI) {89return null;90}9192@Override93public Iterator<String> getPrefixes(String namespaceURI) {94return null;95}96});9798var validator = XMLUtils.validator();99XMLUtils.addPolicy("disallowAlg " + DigestMethod.SHA256);100Element e;101102// 1. Modify the MGF1 digest algorithm in PSSParams to SHA-256103e = (Element) xp.evaluate(104"/a/ds:Signature/ds:SignedInfo/ds:SignatureMethod" +105"/pss:RSAPSSParams/pss:MaskGenerationFunction/ds:DigestMethod",106signed, XPathConstants.NODE);107e.setAttribute("Algorithm", DigestMethod.SHA256);108109// When secureValidation is true, validate() throws an exception110Utils.runAndCheckException(() -> validator.secureValidation(true).validate(signed),111t -> Asserts.assertTrue(t instanceof MarshalException112&& t.getMessage().contains("in MGF1")113&& t.getMessage().contains(DigestMethod.SHA256), Objects.toString(t)));114// When secureValidation is false, validate() returns false115Asserts.assertFalse(validator.secureValidation(false).validate(signed));116117// Revert the change and confirm validate() returns true118e.setAttribute("Algorithm", DigestMethod.SHA512);119Asserts.assertTrue(validator.secureValidation(true).validate(signed));120121// 2. Modify the digest algorithm in PSSParams to SHA-256122e = (Element) xp.evaluate(123"/a/ds:Signature/ds:SignedInfo/ds:SignatureMethod" +124"/pss:RSAPSSParams/ds:DigestMethod",125signed, XPathConstants.NODE);126e.setAttribute("Algorithm", DigestMethod.SHA256);127128// When secureValidation is true, validate() throws an exception129Utils.runAndCheckException(() -> validator.secureValidation(true).validate(signed),130t -> Asserts.assertTrue(t instanceof MarshalException131&& t.getMessage().contains("in PSS")132&& t.getMessage().contains(DigestMethod.SHA256), Objects.toString(t)));133// When secureValidation is false, validate() returns false134Asserts.assertFalse(validator.secureValidation(false).validate(signed));135136// 3. Modify the digest algorithm in PSSParams to SHA-512137e.setAttribute("Algorithm", DigestMethod.SHA512);138139// No matter if secureValidation is true or false, validate()140// returns false. This means the policy allows it.141Asserts.assertFalse(validator.secureValidation(true).validate(signed));142Asserts.assertFalse(validator.secureValidation(false).validate(signed));143}144}145146147