Path: blob/master/test/jdk/javax/xml/crypto/dsig/ValidationTests.java
41152 views
/*1* Copyright (c) 2005, 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 4635230 6365103 6366054 6824440 7131084 8046724 807969326* @summary Basic unit tests for validating XML Signatures with JSR 10527* @modules java.base/sun.security.util28* java.base/sun.security.x50929* java.xml.crypto/org.jcp.xml.dsig.internal.dom30* @library /test/lib31* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java32* X509KeySelector.java ValidationTests.java33* @run main/othervm ValidationTests34* @author Sean Mullan35*/36import java.io.File;37import java.io.FileInputStream;38import java.security.*;39import javax.xml.crypto.Data;40import javax.xml.crypto.KeySelector;41import javax.xml.crypto.MarshalException;42import javax.xml.crypto.OctetStreamData;43import javax.xml.crypto.URIDereferencer;44import javax.xml.crypto.URIReference;45import javax.xml.crypto.URIReferenceException;46import javax.xml.crypto.XMLCryptoContext;47import javax.xml.crypto.dsig.XMLSignatureException;48import javax.xml.crypto.dsig.XMLSignatureFactory;4950import jdk.test.lib.security.SecurityUtils;5152public class ValidationTests {5354private static SignatureValidator validator;55private final static String DIR = System.getProperty("test.src", ".");56private final static String DATA_DIR =57DIR + System.getProperty("file.separator") + "data";58private final static String KEYSTORE =59DATA_DIR + System.getProperty("file.separator") + "certs" +60System.getProperty("file.separator") + "xmldsig.jks";61private final static String STYLESHEET =62"http://www.w3.org/TR/xml-stylesheet";63private final static String STYLESHEET_B64 =64"http://www.w3.org/Signature/2002/04/xml-stylesheet.b64";6566static class Test {67String file;68KeySelector ks;69Class exception;7071Test(String file, KeySelector ks, Class exception) {72this.file = file;73this.ks = ks;74this.exception = exception;75}7677// XMLSignatureException is expected by default78Test(String file, KeySelector ks) {79this(file, ks, XMLSignatureException.class);80}81}8283static KeySelector skks;84static {85try {86skks =87new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"));88} catch (Exception e) {89//should not occur90}91}92private final static KeySelector SKKS = skks;93private final static KeySelector KVKS =94new KeySelectors.KeyValueKeySelector();95private final static KeySelector CKS =96new KeySelectors.CollectionKeySelector(new File(DATA_DIR));97private final static KeySelector RXKS =98new KeySelectors.RawX509KeySelector();99private final static KeySelector XKS = null;100private static URIDereferencer httpUd = null;101102private final static Test[] VALID_TESTS = {103new Test("signature-enveloped-dsa.xml", KVKS),104new Test("signature-enveloping-b64-dsa.xml", KVKS),105new Test("signature-enveloping-dsa.xml", KVKS),106new Test("signature-enveloping-rsa.xml", KVKS),107new Test("signature-enveloping-p256-sha1.xml", KVKS),108new Test("signature-enveloping-p384-sha1.xml", KVKS),109new Test("signature-enveloping-p521-sha1.xml", KVKS),110new Test("signature-enveloping-hmac-sha1.xml", SKKS),111new Test("signature-external-dsa.xml", KVKS),112new Test("signature-external-b64-dsa.xml", KVKS),113new Test("signature-retrievalmethod-rawx509crt.xml", CKS),114new Test("signature-keyname.xml", CKS),115new Test("signature-x509-crt-crl.xml", RXKS),116new Test("signature-x509-crt.xml", RXKS),117new Test("signature-x509-is.xml", CKS),118new Test("signature-x509-ski.xml", CKS),119new Test("signature-x509-sn.xml", CKS),120new Test("signature.xml", XKS),121new Test("exc-signature.xml", KVKS),122new Test("sign-spec.xml", RXKS),123new Test("xmldsig-xfilter2.xml", KVKS)124};125126private final static Test[] INVALID_TESTS = {127new Test("signature-enveloping-hmac-sha1-40.xml", SKKS),128new Test("signature-enveloping-hmac-sha1-trunclen-0-attack.xml", SKKS),129new Test("signature-enveloping-hmac-sha1-trunclen-8-attack.xml", SKKS),130new Test("signature-extra-text-in-signed-info.xml", SKKS,131MarshalException.class),132new Test("signature-wrong-canonicalization-method-algorithm.xml", SKKS,133MarshalException.class),134new Test("signature-wrong-transform-algorithm.xml", SKKS,135MarshalException.class),136new Test("signature-no-reference-uri.xml", SKKS),137new Test("signature-wrong-signature-method-algorithm.xml", SKKS,138MarshalException.class),139new Test("signature-wrong-tag-names.xml", SKKS, MarshalException.class)140};141142public static void main(String args[]) throws Exception {143// Re-enable sha1 algs144SecurityUtils.removeAlgsFromDSigPolicy("sha1");145146httpUd = new HttpURIDereferencer();147148validator = new SignatureValidator(new File(DATA_DIR));149150boolean atLeastOneFailed = false;151for (Test test : VALID_TESTS) {152System.out.println("Validating " + test.file);153if (test_signature(test)) {154System.out.println("PASSED");155} else {156System.out.println("FAILED");157atLeastOneFailed = true;158}159}160// test with reference caching enabled161System.out.println("Validating sign-spec.xml with caching enabled");162if (test_signature(new Test("sign-spec.xml", RXKS), true)) {163System.out.println("PASSED");164} else {165System.out.println("FAILED");166atLeastOneFailed = true;167}168169for (Test test : INVALID_TESTS) {170System.out.println("Validating " + test.file);171try {172test_signature(test);173System.out.println("FAILED");174atLeastOneFailed = true;175} catch (Exception e) {176System.out.println("Exception: " + e);177if (e.getClass() != test.exception) {178System.out.println("FAILED: unexpected exception");179atLeastOneFailed = true;180} else {181System.out.println("PASSED");182}183}184}185186if (atLeastOneFailed) {187throw new Exception188("At least one signature did not validate as expected");189}190}191192public static boolean test_signature(Test test) throws Exception {193return test_signature(test, false);194}195196public static boolean test_signature(Test test, boolean cache)197throws Exception198{199if (test.ks == null) {200KeyStore keystore = KeyStore.getInstance("JKS");201try (FileInputStream fis = new FileInputStream(KEYSTORE)) {202keystore.load(fis, "changeit".toCharArray());203test.ks = new X509KeySelector(keystore, false);204}205}206return validator.validate(test.file, test.ks, httpUd, cache);207}208209/**210* This URIDereferencer returns locally cached copies of http content to211* avoid test failures due to network glitches, etc.212*/213private static class HttpURIDereferencer implements URIDereferencer {214private URIDereferencer defaultUd;215216HttpURIDereferencer() {217defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer();218}219220public Data dereference(final URIReference ref, XMLCryptoContext ctx)221throws URIReferenceException {222String uri = ref.getURI();223if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) {224try {225FileInputStream fis = new FileInputStream(new File226(DATA_DIR, uri.substring(uri.lastIndexOf('/'))));227return new OctetStreamData(fis,ref.getURI(),ref.getType());228} catch (Exception e) { throw new URIReferenceException(e); }229}230231// fallback on builtin deref232return defaultUd.dereference(ref, ctx);233}234}235}236237238