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