Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/xml/crypto/dsig/ValidationTests.java
41152 views
1
/*
2
* Copyright (c) 2005, 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 4635230 6365103 6366054 6824440 7131084 8046724 8079693
27
* @summary Basic unit tests for validating XML Signatures with JSR 105
28
* @modules java.base/sun.security.util
29
* java.base/sun.security.x509
30
* java.xml.crypto/org.jcp.xml.dsig.internal.dom
31
* @library /test/lib
32
* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java
33
* X509KeySelector.java ValidationTests.java
34
* @run main/othervm ValidationTests
35
* @author Sean Mullan
36
*/
37
import java.io.File;
38
import java.io.FileInputStream;
39
import java.security.*;
40
import javax.xml.crypto.Data;
41
import javax.xml.crypto.KeySelector;
42
import javax.xml.crypto.MarshalException;
43
import javax.xml.crypto.OctetStreamData;
44
import javax.xml.crypto.URIDereferencer;
45
import javax.xml.crypto.URIReference;
46
import javax.xml.crypto.URIReferenceException;
47
import javax.xml.crypto.XMLCryptoContext;
48
import javax.xml.crypto.dsig.XMLSignatureException;
49
import javax.xml.crypto.dsig.XMLSignatureFactory;
50
51
import jdk.test.lib.security.SecurityUtils;
52
53
public class ValidationTests {
54
55
private static SignatureValidator validator;
56
private final static String DIR = System.getProperty("test.src", ".");
57
private final static String DATA_DIR =
58
DIR + System.getProperty("file.separator") + "data";
59
private final static String KEYSTORE =
60
DATA_DIR + System.getProperty("file.separator") + "certs" +
61
System.getProperty("file.separator") + "xmldsig.jks";
62
private final static String STYLESHEET =
63
"http://www.w3.org/TR/xml-stylesheet";
64
private final static String STYLESHEET_B64 =
65
"http://www.w3.org/Signature/2002/04/xml-stylesheet.b64";
66
67
static class Test {
68
String file;
69
KeySelector ks;
70
Class exception;
71
72
Test(String file, KeySelector ks, Class exception) {
73
this.file = file;
74
this.ks = ks;
75
this.exception = exception;
76
}
77
78
// XMLSignatureException is expected by default
79
Test(String file, KeySelector ks) {
80
this(file, ks, XMLSignatureException.class);
81
}
82
}
83
84
static KeySelector skks;
85
static {
86
try {
87
skks =
88
new KeySelectors.SecretKeySelector("secret".getBytes("ASCII"));
89
} catch (Exception e) {
90
//should not occur
91
}
92
}
93
private final static KeySelector SKKS = skks;
94
private final static KeySelector KVKS =
95
new KeySelectors.KeyValueKeySelector();
96
private final static KeySelector CKS =
97
new KeySelectors.CollectionKeySelector(new File(DATA_DIR));
98
private final static KeySelector RXKS =
99
new KeySelectors.RawX509KeySelector();
100
private final static KeySelector XKS = null;
101
private static URIDereferencer httpUd = null;
102
103
private final static Test[] VALID_TESTS = {
104
new Test("signature-enveloped-dsa.xml", KVKS),
105
new Test("signature-enveloping-b64-dsa.xml", KVKS),
106
new Test("signature-enveloping-dsa.xml", KVKS),
107
new Test("signature-enveloping-rsa.xml", KVKS),
108
new Test("signature-enveloping-p256-sha1.xml", KVKS),
109
new Test("signature-enveloping-p384-sha1.xml", KVKS),
110
new Test("signature-enveloping-p521-sha1.xml", KVKS),
111
new Test("signature-enveloping-hmac-sha1.xml", SKKS),
112
new Test("signature-external-dsa.xml", KVKS),
113
new Test("signature-external-b64-dsa.xml", KVKS),
114
new Test("signature-retrievalmethod-rawx509crt.xml", CKS),
115
new Test("signature-keyname.xml", CKS),
116
new Test("signature-x509-crt-crl.xml", RXKS),
117
new Test("signature-x509-crt.xml", RXKS),
118
new Test("signature-x509-is.xml", CKS),
119
new Test("signature-x509-ski.xml", CKS),
120
new Test("signature-x509-sn.xml", CKS),
121
new Test("signature.xml", XKS),
122
new Test("exc-signature.xml", KVKS),
123
new Test("sign-spec.xml", RXKS),
124
new Test("xmldsig-xfilter2.xml", KVKS)
125
};
126
127
private final static Test[] INVALID_TESTS = {
128
new Test("signature-enveloping-hmac-sha1-40.xml", SKKS),
129
new Test("signature-enveloping-hmac-sha1-trunclen-0-attack.xml", SKKS),
130
new Test("signature-enveloping-hmac-sha1-trunclen-8-attack.xml", SKKS),
131
new Test("signature-extra-text-in-signed-info.xml", SKKS,
132
MarshalException.class),
133
new Test("signature-wrong-canonicalization-method-algorithm.xml", SKKS,
134
MarshalException.class),
135
new Test("signature-wrong-transform-algorithm.xml", SKKS,
136
MarshalException.class),
137
new Test("signature-no-reference-uri.xml", SKKS),
138
new Test("signature-wrong-signature-method-algorithm.xml", SKKS,
139
MarshalException.class),
140
new Test("signature-wrong-tag-names.xml", SKKS, MarshalException.class)
141
};
142
143
public static void main(String args[]) throws Exception {
144
// Re-enable sha1 algs
145
SecurityUtils.removeAlgsFromDSigPolicy("sha1");
146
147
httpUd = new HttpURIDereferencer();
148
149
validator = new SignatureValidator(new File(DATA_DIR));
150
151
boolean atLeastOneFailed = false;
152
for (Test test : VALID_TESTS) {
153
System.out.println("Validating " + test.file);
154
if (test_signature(test)) {
155
System.out.println("PASSED");
156
} else {
157
System.out.println("FAILED");
158
atLeastOneFailed = true;
159
}
160
}
161
// test with reference caching enabled
162
System.out.println("Validating sign-spec.xml with caching enabled");
163
if (test_signature(new Test("sign-spec.xml", RXKS), true)) {
164
System.out.println("PASSED");
165
} else {
166
System.out.println("FAILED");
167
atLeastOneFailed = true;
168
}
169
170
for (Test test : INVALID_TESTS) {
171
System.out.println("Validating " + test.file);
172
try {
173
test_signature(test);
174
System.out.println("FAILED");
175
atLeastOneFailed = true;
176
} catch (Exception e) {
177
System.out.println("Exception: " + e);
178
if (e.getClass() != test.exception) {
179
System.out.println("FAILED: unexpected exception");
180
atLeastOneFailed = true;
181
} else {
182
System.out.println("PASSED");
183
}
184
}
185
}
186
187
if (atLeastOneFailed) {
188
throw new Exception
189
("At least one signature did not validate as expected");
190
}
191
}
192
193
public static boolean test_signature(Test test) throws Exception {
194
return test_signature(test, false);
195
}
196
197
public static boolean test_signature(Test test, boolean cache)
198
throws Exception
199
{
200
if (test.ks == null) {
201
KeyStore keystore = KeyStore.getInstance("JKS");
202
try (FileInputStream fis = new FileInputStream(KEYSTORE)) {
203
keystore.load(fis, "changeit".toCharArray());
204
test.ks = new X509KeySelector(keystore, false);
205
}
206
}
207
return validator.validate(test.file, test.ks, httpUd, cache);
208
}
209
210
/**
211
* This URIDereferencer returns locally cached copies of http content to
212
* avoid test failures due to network glitches, etc.
213
*/
214
private static class HttpURIDereferencer implements URIDereferencer {
215
private URIDereferencer defaultUd;
216
217
HttpURIDereferencer() {
218
defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer();
219
}
220
221
public Data dereference(final URIReference ref, XMLCryptoContext ctx)
222
throws URIReferenceException {
223
String uri = ref.getURI();
224
if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) {
225
try {
226
FileInputStream fis = new FileInputStream(new File
227
(DATA_DIR, uri.substring(uri.lastIndexOf('/'))));
228
return new OctetStreamData(fis,ref.getURI(),ref.getType());
229
} catch (Exception e) { throw new URIReferenceException(e); }
230
}
231
232
// fallback on builtin deref
233
return defaultUd.dereference(ref, ctx);
234
}
235
}
236
}
237
238