Skip to content

Commit baf46ba

Browse files
committedJan 28, 2021
8259801: Enable XML Signature secure validation mode by default
Reviewed-by: weijun, rhalade
1 parent 20e7df5 commit baf46ba

File tree

3 files changed

+52
-43
lines changed

3 files changed

+52
-43
lines changed
 

‎src/java.base/share/conf/security/java.security

+7-6
Original file line numberDiff line numberDiff line change
@@ -925,10 +925,11 @@ jdk.tls.keyLimits=AES/GCM/NoPadding KeyUpdate 2^37
925925
crypto.policy=crypto.policydir-tbd
926926

927927
#
928-
# The policy for the XML Signature secure validation mode. The mode is
929-
# enabled by setting the property "org.jcp.xml.dsig.secureValidation" to
930-
# true with the javax.xml.crypto.XMLCryptoContext.setProperty() method,
931-
# or by running the code with a SecurityManager.
928+
# The policy for the XML Signature secure validation mode. Validation of
929+
# XML Signatures that violate any of these constraints will fail. The
930+
# mode is enforced by default. The mode can be disabled by setting the
931+
# property "org.jcp.xml.dsig.secureValidation" to Boolean.FALSE with the
932+
# javax.xml.crypto.XMLCryptoContext.setProperty() method.
932933
#
933934
# Policy:
934935
# Constraint {"," Constraint }
@@ -955,8 +956,8 @@ crypto.policy=crypto.policydir-tbd
955956
# MaxReferencesConstraint or KeySizeConstraint (for the same key type) is
956957
# specified more than once, only the last entry is enforced.
957958
#
958-
# Note: This property is currently used by the JDK Reference implementation. It
959-
# is not guaranteed to be examined and used by other implementations.
959+
# Note: This property is currently used by the JDK Reference implementation.
960+
# It is not guaranteed to be examined and used by other implementations.
960961
#
961962
jdk.xml.dsig.secureValidationPolicy=\
962963
disallowAlg http://www.w3.org/TR/1999/REC-xslt-19991116,\

‎src/java.xml.crypto/share/classes/javax/xml/crypto/dsig/dom/DOMValidateContext.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,6 +51,14 @@
5151
* (for example, you should not use the same <code>DOMValidateContext</code>
5252
* instance to validate two different {@link XMLSignature} objects).
5353
*
54+
* @implNote
55+
* By default, the JDK implementation enables a secure validation mode by
56+
* setting the <code>org.jcp.xml.dsig.secureValidation</code> property to
57+
* <code>Boolean.TRUE</code> (see the {@link #setProperty setProperty}
58+
* method). When enabled, validation of XML signatures are subject to
59+
* stricter checking of algorithms and other constraints as specified by the
60+
* <code>jdk.xml.dsig.secureValidationPolicy</code> security property.
61+
*
5462
* @author Sean Mullan
5563
* @author JSR 105 Expert Group
5664
* @since 1.6
@@ -103,10 +111,7 @@ private void init(Node node, KeySelector ks) {
103111

104112
this.node = node;
105113
super.setKeySelector(ks);
106-
if (System.getSecurityManager() != null) {
107-
super.setProperty("org.jcp.xml.dsig.secureValidation",
108-
Boolean.TRUE);
109-
}
114+
super.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.TRUE);
110115
}
111116

112117
/**

‎test/jdk/javax/xml/crypto/dsig/GenerationTests.java

+35-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -603,21 +603,21 @@ static void test_create_signature_enveloped_dsa(int size) throws Exception {
603603
static void test_create_signature_enveloping_b64_dsa() throws Exception {
604604
System.out.println("* Generating signature-enveloping-b64-dsa.xml");
605605
test_create_signature_enveloping
606-
(sha1, dsaSha1, dsa1024, signingKey, kvks, true);
606+
(sha1, dsaSha1, dsa1024, signingKey, kvks, true, true);
607607
System.out.println();
608608
}
609609

610610
static void test_create_signature_enveloping_dsa() throws Exception {
611611
System.out.println("* Generating signature-enveloping-dsa.xml");
612612
test_create_signature_enveloping
613-
(sha1, dsaSha1, dsa1024, signingKey, kvks, false);
613+
(sha1, dsaSha1, dsa1024, signingKey, kvks, false, true);
614614
System.out.println();
615615
}
616616

617617
static void test_create_signature_enveloping_sha256_dsa() throws Exception {
618618
System.out.println("* Generating signature-enveloping-sha256-dsa.xml");
619619
test_create_signature_enveloping
620-
(sha256, dsaSha1, dsa1024, signingKey, kvks, false);
620+
(sha256, dsaSha1, dsa1024, signingKey, kvks, false, true);
621621
System.out.println();
622622
}
623623

@@ -626,7 +626,7 @@ static void test_create_signature_enveloping_hmac_sha1_40()
626626
System.out.println("* Generating signature-enveloping-hmac-sha1-40.xml");
627627
try {
628628
test_create_signature_enveloping(sha1, hmacSha1, null,
629-
getSecretKey("secret".getBytes("ASCII")), sks, false);
629+
getSecretKey("secret".getBytes("ASCII")), sks, false, true);
630630
} catch (Exception e) {
631631
if (!(e instanceof XMLSignatureException)) {
632632
throw e;
@@ -639,199 +639,199 @@ static void test_create_signature_enveloping_hmac_sha256()
639639
throws Exception {
640640
System.out.println("* Generating signature-enveloping-hmac-sha256.xml");
641641
test_create_signature_enveloping(sha1, hmacSha256, null,
642-
getSecretKey("secret".getBytes("ASCII")), sks, false);
642+
getSecretKey("secret".getBytes("ASCII")), sks, false, true);
643643
System.out.println();
644644
}
645645

646646
static void test_create_signature_enveloping_hmac_sha224()
647647
throws Exception {
648648
System.out.println("* Generating signature-enveloping-hmac-sha224.xml");
649649
test_create_signature_enveloping(sha1, hmacSha224, null,
650-
getSecretKey("secret".getBytes("ASCII")), sks, false);
650+
getSecretKey("secret".getBytes("ASCII")), sks, false, true);
651651
System.out.println();
652652
}
653653

654654
static void test_create_signature_enveloping_hmac_sha384()
655655
throws Exception {
656656
System.out.println("* Generating signature-enveloping-hmac-sha384.xml");
657657
test_create_signature_enveloping(sha1, hmacSha384, null,
658-
getSecretKey("secret".getBytes("ASCII")), sks, false);
658+
getSecretKey("secret".getBytes("ASCII")), sks, false, true);
659659
System.out.println();
660660
}
661661

662662
static void test_create_signature_enveloping_hmac_sha512()
663663
throws Exception {
664664
System.out.println("* Generating signature-enveloping-hmac-sha512.xml");
665665
test_create_signature_enveloping(sha1, hmacSha512, null,
666-
getSecretKey("secret".getBytes("ASCII")), sks, false);
666+
getSecretKey("secret".getBytes("ASCII")), sks, false, true);
667667
System.out.println();
668668
}
669669

670670
static void test_create_signature_enveloping_rsa() throws Exception {
671671
System.out.println("* Generating signature-enveloping-rsa.xml");
672672
test_create_signature_enveloping(sha1, rsaSha1, rsa,
673-
getPrivateKey("RSA", 512), kvks, false);
673+
getPrivateKey("RSA", 512), kvks, false, false);
674674
System.out.println();
675675
}
676676

677677
static void test_create_signature_enveloping_sha384_rsa_sha256()
678678
throws Exception {
679679
System.out.println("* Generating signature-enveloping-sha384-rsa_sha256.xml");
680680
test_create_signature_enveloping(sha384, rsaSha256, rsa,
681-
getPrivateKey("RSA", 512), kvks, false);
681+
getPrivateKey("RSA", 512), kvks, false, false);
682682
System.out.println();
683683
}
684684

685685
static void test_create_signature_enveloping_sha224_rsa_sha256()
686686
throws Exception {
687687
System.out.println("* Generating signature-enveloping-sha224-rsa_sha256.xml");
688688
test_create_signature_enveloping(sha224, rsaSha256, rsa,
689-
getPrivateKey("RSA", 512), kvks, false);
689+
getPrivateKey("RSA", 512), kvks, false, false);
690690
System.out.println();
691691
}
692692

693693
static void test_create_signature_enveloping_sha3_224_rsa_sha256()
694694
throws Exception {
695695
System.out.println("* Generating signature-enveloping-sha3_224-rsa_sha256.xml");
696696
test_create_signature_enveloping(sha3_224, rsaSha256, rsa,
697-
getPrivateKey("RSA", 512), kvks, false);
697+
getPrivateKey("RSA", 512), kvks, false, false);
698698
System.out.println();
699699
}
700700

701701
static void test_create_signature_enveloping_sha3_256_rsa_sha256()
702702
throws Exception {
703703
System.out.println("* Generating signature-enveloping-sha3_256-rsa_sha256.xml");
704704
test_create_signature_enveloping(sha3_256, rsaSha256, rsa,
705-
getPrivateKey("RSA", 512), kvks, false);
705+
getPrivateKey("RSA", 512), kvks, false, false);
706706
System.out.println();
707707
}
708708

709709
static void test_create_signature_enveloping_sha3_384_rsa_sha256()
710710
throws Exception {
711711
System.out.println("* Generating signature-enveloping-sha3_384-rsa_sha256.xml");
712712
test_create_signature_enveloping(sha3_384, rsaSha256, rsa,
713-
getPrivateKey("RSA", 512), kvks, false);
713+
getPrivateKey("RSA", 512), kvks, false, false);
714714
System.out.println();
715715
}
716716

717717
static void test_create_signature_enveloping_sha3_512_rsa_sha256()
718718
throws Exception {
719719
System.out.println("* Generating signature-enveloping-sha3_512-rsa_sha256.xml");
720720
test_create_signature_enveloping(sha3_512, rsaSha256, rsa,
721-
getPrivateKey("RSA", 512), kvks, false);
721+
getPrivateKey("RSA", 512), kvks, false, false);
722722
System.out.println();
723723
}
724724

725725
static void test_create_signature_enveloping_sha512_rsa_sha384()
726726
throws Exception {
727727
System.out.println("* Generating signature-enveloping-sha512-rsa_sha384.xml");
728728
test_create_signature_enveloping(sha512, rsaSha384, rsa1024,
729-
getPrivateKey("RSA", 1024), kvks, false);
729+
getPrivateKey("RSA", 1024), kvks, false, true);
730730
System.out.println();
731731
}
732732

733733
static void test_create_signature_enveloping_sha512_rsa_sha224()
734734
throws Exception {
735735
System.out.println("* Generating signature-enveloping-sha512-rsa_sha224.xml");
736736
test_create_signature_enveloping(sha512, rsaSha224, rsa1024,
737-
getPrivateKey("RSA", 1024), kvks, false);
737+
getPrivateKey("RSA", 1024), kvks, false, true);
738738
System.out.println();
739739
}
740740

741741
static void test_create_signature_enveloping_sha512_rsa_sha512()
742742
throws Exception {
743743
System.out.println("* Generating signature-enveloping-sha512-rsa_sha512.xml");
744744
test_create_signature_enveloping(sha512, rsaSha512, rsa1024,
745-
getPrivateKey("RSA", 1024), kvks, false);
745+
getPrivateKey("RSA", 1024), kvks, false, true);
746746
System.out.println();
747747
}
748748

749749
static void test_create_signature_enveloping_sha512_rsa_sha1_mgf1()
750750
throws Exception {
751751
System.out.println("* Generating signature-enveloping-sha512-rsa_sha1_mgf1.xml");
752752
test_create_signature_enveloping(sha512, rsaSha1mgf1, rsa1024,
753-
getPrivateKey("RSA", 1024), kvks, false);
753+
getPrivateKey("RSA", 1024), kvks, false, true);
754754
System.out.println();
755755
}
756756

757757
static void test_create_signature_enveloping_sha512_rsa_sha224_mgf1()
758758
throws Exception {
759759
System.out.println("* Generating signature-enveloping-sha512-rsa_sha224_mgf1.xml");
760760
test_create_signature_enveloping(sha512, rsaSha224mgf1, rsa1024,
761-
getPrivateKey("RSA", 1024), kvks, false);
761+
getPrivateKey("RSA", 1024), kvks, false, true);
762762
System.out.println();
763763
}
764764

765765
static void test_create_signature_enveloping_sha512_rsa_sha256_mgf1()
766766
throws Exception {
767767
System.out.println("* Generating signature-enveloping-sha512-rsa_sha256_mgf1.xml");
768768
test_create_signature_enveloping(sha512, rsaSha256mgf1, rsa1024,
769-
getPrivateKey("RSA", 1024), kvks, false);
769+
getPrivateKey("RSA", 1024), kvks, false, true);
770770
System.out.println();
771771
}
772772

773773
static void test_create_signature_enveloping_sha512_rsa_sha384_mgf1()
774774
throws Exception {
775775
System.out.println("* Generating signature-enveloping-sha512-rsa_sha384_mgf1.xml");
776776
test_create_signature_enveloping(sha512, rsaSha384mgf1, rsa1024,
777-
getPrivateKey("RSA", 1024), kvks, false);
777+
getPrivateKey("RSA", 1024), kvks, false, true);
778778
System.out.println();
779779
}
780780

781781
static void test_create_signature_enveloping_sha512_rsa_sha512_mgf1()
782782
throws Exception {
783783
System.out.println("* Generating signature-enveloping-sha512-rsa_sha512_mgf1.xml");
784784
test_create_signature_enveloping(sha512, rsaSha512mgf1, rsa2048,
785-
getPrivateKey("RSA", 2048), kvks, false);
785+
getPrivateKey("RSA", 2048), kvks, false, true);
786786
System.out.println();
787787
}
788788

789789
static void test_create_signature_enveloping_p256_sha1() throws Exception {
790790
System.out.println("* Generating signature-enveloping-p256-sha1.xml");
791791
test_create_signature_enveloping(sha1, ecdsaSha1, p256ki,
792-
getECPrivateKey("P256"), kvks, false);
792+
getECPrivateKey("P256"), kvks, false, true);
793793
System.out.println();
794794
}
795795

796796
static void test_create_signature_enveloping_p256_sha224() throws Exception {
797797
System.out.println("* Generating signature-enveloping-p256-sha224.xml");
798798
test_create_signature_enveloping(sha1, ecdsaSha224, p256ki,
799-
getECPrivateKey("P256"), kvks, false);
799+
getECPrivateKey("P256"), kvks, false, true);
800800
System.out.println();
801801
}
802802

803803
static void test_create_signature_enveloping_p256_sha256() throws Exception {
804804
System.out.println("* Generating signature-enveloping-p256-sha256.xml");
805805
test_create_signature_enveloping(sha1, ecdsaSha256, p256ki,
806-
getECPrivateKey("P256"), kvks, false);
806+
getECPrivateKey("P256"), kvks, false, true);
807807
System.out.println();
808808
}
809809

810810
static void test_create_signature_enveloping_p256_sha384() throws Exception {
811811
System.out.println("* Generating signature-enveloping-p256-sha384.xml");
812812
test_create_signature_enveloping(sha1, ecdsaSha384, p256ki,
813-
getECPrivateKey("P256"), kvks, false);
813+
getECPrivateKey("P256"), kvks, false, true);
814814
System.out.println();
815815
}
816816

817817
static void test_create_signature_enveloping_p256_sha512() throws Exception {
818818
System.out.println("* Generating signature-enveloping-p256-sha512.xml");
819819
test_create_signature_enveloping(sha1, ecdsaSha512, p256ki,
820-
getECPrivateKey("P256"), kvks, false);
820+
getECPrivateKey("P256"), kvks, false, true);
821821
System.out.println();
822822
}
823823

824824
static void test_create_signature_enveloping_p384_sha1() throws Exception {
825825
System.out.println("* Generating signature-enveloping-p384-sha1.xml");
826826
test_create_signature_enveloping(sha1, ecdsaSha1, p384ki,
827-
getECPrivateKey("P384"), kvks, false);
827+
getECPrivateKey("P384"), kvks, false, true);
828828
System.out.println();
829829
}
830830

831831
static void test_create_signature_enveloping_p521_sha1() throws Exception {
832832
System.out.println("* Generating signature-enveloping-p521-sha1.xml");
833833
test_create_signature_enveloping(sha1, ecdsaSha1, p521ki,
834-
getECPrivateKey("P521"), kvks, false);
834+
getECPrivateKey("P521"), kvks, false, true);
835835
System.out.println();
836836
}
837837

@@ -963,6 +963,7 @@ static void test_create_signature_reference_dependency() throws Exception {
963963

964964
DOMValidateContext dvc = new DOMValidateContext
965965
(kvks, doc.getDocumentElement());
966+
dvc.setProperty("org.jcp.xml.dsig.secureValidation", false);
966967
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
967968

968969
if (sig.equals(sig2) == false) {
@@ -1010,6 +1011,7 @@ static void test_create_signature_with_attr_in_no_namespace()
10101011

10111012
DOMValidateContext dvc = new DOMValidateContext
10121013
(kvks, doc.getDocumentElement());
1014+
dvc.setProperty("org.jcp.xml.dsig.secureValidation", false);
10131015
dvc.setIdAttributeNS(nc, null, "Id");
10141016
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
10151017

@@ -1451,7 +1453,7 @@ private static void dumpDocument(Document doc, Writer w) throws Exception {
14511453

14521454
private static void test_create_signature_enveloping
14531455
(DigestMethod dm, SignatureMethod sm, KeyInfo ki, Key signingKey,
1454-
KeySelector ks, boolean b64) throws Exception {
1456+
KeySelector ks, boolean b64, boolean secVal) throws Exception {
14551457

14561458
// create reference
14571459
Reference ref;
@@ -1486,6 +1488,7 @@ private static void dumpDocument(Document doc, Writer w) throws Exception {
14861488

14871489
DOMValidateContext dvc = new DOMValidateContext
14881490
(ks, doc.getDocumentElement());
1491+
dvc.setProperty("org.jcp.xml.dsig.secureValidation", secVal);
14891492
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
14901493

14911494
if (sig.equals(sig2) == false) {

0 commit comments

Comments
 (0)
Please sign in to comment.