Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/xml/crypto/dsig/SecureValidation.java
41152 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 8241306
27
* @summary Tests for the jdk.xml.dsig.secureValidationPolicy security property
28
* on the RSASSA-PSS signature method
29
* @library /test/lib
30
* @modules java.base/sun.security.tools.keytool
31
* java.base/sun.security.x509
32
*/
33
import jdk.test.lib.Asserts;
34
import jdk.test.lib.security.XMLUtils;
35
import jdk.test.lib.Utils;
36
import org.w3c.dom.Document;
37
import org.w3c.dom.Element;
38
import sun.security.tools.keytool.CertAndKeyGen;
39
import sun.security.x509.X500Name;
40
41
import javax.xml.crypto.MarshalException;
42
import javax.xml.crypto.dsig.DigestMethod;
43
import javax.xml.crypto.dsig.SignatureMethod;
44
import javax.xml.crypto.dsig.spec.RSAPSSParameterSpec;
45
import javax.xml.namespace.NamespaceContext;
46
import javax.xml.xpath.XPath;
47
import javax.xml.xpath.XPathConstants;
48
import javax.xml.xpath.XPathFactory;
49
import java.security.PrivateKey;
50
import java.security.cert.X509Certificate;
51
import java.security.spec.MGF1ParameterSpec;
52
import java.security.spec.PSSParameterSpec;
53
import java.util.Iterator;
54
import java.util.Objects;
55
56
import static java.security.spec.PSSParameterSpec.TRAILER_FIELD_BC;
57
58
public class SecureValidation {
59
60
public static void main(String[] args) throws Exception {
61
62
Document doc = XMLUtils.string2doc("<a><b>Text</b>Raw</a>");
63
64
CertAndKeyGen g = new CertAndKeyGen("RSASSA-PSS", "RSASSA-PSS");
65
g.generate(2048);
66
X509Certificate cert = g.getSelfCertificate(new X500Name("CN=Me"), 100);
67
PrivateKey privateKey = g.getPrivateKey();
68
PSSParameterSpec pspec = new PSSParameterSpec("SHA-384", "MGF1",
69
MGF1ParameterSpec.SHA512, 48, TRAILER_FIELD_BC);
70
71
// Sign with PSS with SHA-384 and SHA-512
72
Document signed = XMLUtils.signer(privateKey, cert)
73
.dm(DigestMethod.SHA384)
74
.sm(SignatureMethod.RSA_PSS, new RSAPSSParameterSpec(pspec))
75
.sign(doc);
76
77
XPath xp = XPathFactory.newInstance().newXPath();
78
xp.setNamespaceContext(new NamespaceContext() {
79
@Override
80
public String getNamespaceURI(String prefix) {
81
return switch (prefix) {
82
case "ds" -> "http://www.w3.org/2000/09/xmldsig#";
83
case "pss" -> "http://www.w3.org/2007/05/xmldsig-more#";
84
default -> throw new IllegalArgumentException();
85
};
86
}
87
88
@Override
89
public String getPrefix(String namespaceURI) {
90
return null;
91
}
92
93
@Override
94
public Iterator<String> getPrefixes(String namespaceURI) {
95
return null;
96
}
97
});
98
99
var validator = XMLUtils.validator();
100
XMLUtils.addPolicy("disallowAlg " + DigestMethod.SHA256);
101
Element e;
102
103
// 1. Modify the MGF1 digest algorithm in PSSParams to SHA-256
104
e = (Element) xp.evaluate(
105
"/a/ds:Signature/ds:SignedInfo/ds:SignatureMethod" +
106
"/pss:RSAPSSParams/pss:MaskGenerationFunction/ds:DigestMethod",
107
signed, XPathConstants.NODE);
108
e.setAttribute("Algorithm", DigestMethod.SHA256);
109
110
// When secureValidation is true, validate() throws an exception
111
Utils.runAndCheckException(() -> validator.secureValidation(true).validate(signed),
112
t -> Asserts.assertTrue(t instanceof MarshalException
113
&& t.getMessage().contains("in MGF1")
114
&& t.getMessage().contains(DigestMethod.SHA256), Objects.toString(t)));
115
// When secureValidation is false, validate() returns false
116
Asserts.assertFalse(validator.secureValidation(false).validate(signed));
117
118
// Revert the change and confirm validate() returns true
119
e.setAttribute("Algorithm", DigestMethod.SHA512);
120
Asserts.assertTrue(validator.secureValidation(true).validate(signed));
121
122
// 2. Modify the digest algorithm in PSSParams to SHA-256
123
e = (Element) xp.evaluate(
124
"/a/ds:Signature/ds:SignedInfo/ds:SignatureMethod" +
125
"/pss:RSAPSSParams/ds:DigestMethod",
126
signed, XPathConstants.NODE);
127
e.setAttribute("Algorithm", DigestMethod.SHA256);
128
129
// When secureValidation is true, validate() throws an exception
130
Utils.runAndCheckException(() -> validator.secureValidation(true).validate(signed),
131
t -> Asserts.assertTrue(t instanceof MarshalException
132
&& t.getMessage().contains("in PSS")
133
&& t.getMessage().contains(DigestMethod.SHA256), Objects.toString(t)));
134
// When secureValidation is false, validate() returns false
135
Asserts.assertFalse(validator.secureValidation(false).validate(signed));
136
137
// 3. Modify the digest algorithm in PSSParams to SHA-512
138
e.setAttribute("Algorithm", DigestMethod.SHA512);
139
140
// No matter if secureValidation is true or false, validate()
141
// returns false. This means the policy allows it.
142
Asserts.assertFalse(validator.secureValidation(true).validate(signed));
143
Asserts.assertFalse(validator.secureValidation(false).validate(signed));
144
}
145
}
146
147