author | chegar |
Thu, 06 May 2010 17:17:09 +0100 | |
changeset 5463 | ad2d35ddcf37 |
parent 4506 | 402b248b41de |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
3462 | 2 |
* Copyright 2005-2009 Sun Microsystems, Inc. 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 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/** |
|
25 |
* @test |
|
4506
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
26 |
* @bug 4635230 6283345 6303830 6824440 6867348 |
2 | 27 |
* @summary Basic unit tests for generating XML Signatures with JSR 105 |
28 |
* @compile -XDignore.symbol.file KeySelectors.java SignatureValidator.java |
|
29 |
* X509KeySelector.java GenerationTests.java |
|
30 |
* @run main GenerationTests |
|
31 |
* @author Sean Mullan |
|
32 |
*/ |
|
33 |
||
34 |
import java.io.*; |
|
35 |
import java.math.BigInteger; |
|
36 |
import java.security.Key; |
|
37 |
import java.security.KeyFactory; |
|
38 |
import java.security.KeyStore; |
|
39 |
import java.security.PrivateKey; |
|
40 |
import java.security.PublicKey; |
|
41 |
import java.security.cert.Certificate; |
|
42 |
import java.security.cert.CertificateFactory; |
|
43 |
import java.security.cert.X509Certificate; |
|
44 |
import java.security.cert.X509CRL; |
|
45 |
import java.security.spec.KeySpec; |
|
46 |
import java.security.spec.DSAPrivateKeySpec; |
|
47 |
import java.security.spec.DSAPublicKeySpec; |
|
48 |
import java.security.spec.RSAPrivateKeySpec; |
|
49 |
import java.security.spec.RSAPublicKeySpec; |
|
50 |
import java.util.*; |
|
51 |
import javax.crypto.SecretKey; |
|
52 |
import javax.xml.XMLConstants; |
|
53 |
import javax.xml.parsers.*; |
|
54 |
import org.w3c.dom.*; |
|
55 |
import javax.xml.crypto.Data; |
|
56 |
import javax.xml.crypto.KeySelector; |
|
57 |
import javax.xml.crypto.OctetStreamData; |
|
58 |
import javax.xml.crypto.URIDereferencer; |
|
59 |
import javax.xml.crypto.URIReference; |
|
60 |
import javax.xml.crypto.URIReferenceException; |
|
61 |
import javax.xml.crypto.XMLCryptoContext; |
|
62 |
import javax.xml.crypto.XMLStructure; |
|
63 |
import javax.xml.crypto.dsig.*; |
|
64 |
import javax.xml.crypto.dom.*; |
|
65 |
import javax.xml.crypto.dsig.dom.DOMSignContext; |
|
66 |
import javax.xml.crypto.dsig.dom.DOMValidateContext; |
|
67 |
import javax.xml.crypto.dsig.keyinfo.*; |
|
68 |
import javax.xml.crypto.dsig.spec.*; |
|
69 |
import javax.xml.transform.*; |
|
70 |
import javax.xml.transform.dom.DOMSource; |
|
71 |
import javax.xml.transform.stream.StreamResult; |
|
72 |
||
73 |
/** |
|
74 |
* Test that recreates merlin-xmldsig-twenty-three test vectors but with |
|
75 |
* different keys and X.509 data. |
|
76 |
*/ |
|
77 |
public class GenerationTests { |
|
78 |
||
79 |
private static XMLSignatureFactory fac; |
|
80 |
private static KeyInfoFactory kifac; |
|
81 |
private static DocumentBuilder db; |
|
82 |
private static CanonicalizationMethod withoutComments; |
|
83 |
private static SignatureMethod dsaSha1, rsaSha1, rsaSha256, rsaSha384, rsaSha512; |
|
84 |
private static DigestMethod sha1, sha256, sha384, sha512; |
|
85 |
private static KeyInfo dsa, rsa, rsa1024; |
|
86 |
private static KeySelector kvks = new KeySelectors.KeyValueKeySelector(); |
|
87 |
private static KeySelector sks; |
|
88 |
private static Key signingKey; |
|
89 |
private static PublicKey validatingKey; |
|
90 |
private static Certificate signingCert; |
|
91 |
private static KeyStore ks; |
|
92 |
private final static String DIR = System.getProperty("test.src", "."); |
|
1337 | 93 |
// private final static String DIR = "."; |
2 | 94 |
private final static String DATA_DIR = |
95 |
DIR + System.getProperty("file.separator") + "data"; |
|
96 |
private final static String KEYSTORE = |
|
97 |
DATA_DIR + System.getProperty("file.separator") + "certs" + |
|
98 |
System.getProperty("file.separator") + "test.jks"; |
|
99 |
private final static String CRL = |
|
100 |
DATA_DIR + System.getProperty("file.separator") + "certs" + |
|
101 |
System.getProperty("file.separator") + "crl"; |
|
102 |
private final static String ENVELOPE = |
|
103 |
DATA_DIR + System.getProperty("file.separator") + "envelope.xml"; |
|
104 |
private static URIDereferencer httpUd = null; |
|
105 |
private final static String STYLESHEET = |
|
106 |
"http://www.w3.org/TR/xml-stylesheet"; |
|
107 |
private final static String STYLESHEET_B64 = |
|
108 |
"http://www.w3.org/Signature/2002/04/xml-stylesheet.b64"; |
|
109 |
||
110 |
public static void main(String args[]) throws Exception { |
|
111 |
setup(); |
|
112 |
test_create_signature_enveloped_dsa(); |
|
113 |
test_create_signature_enveloping_b64_dsa(); |
|
114 |
test_create_signature_enveloping_dsa(); |
|
115 |
test_create_signature_enveloping_hmac_sha1_40(); |
|
116 |
test_create_signature_enveloping_hmac_sha256(); |
|
117 |
test_create_signature_enveloping_hmac_sha384(); |
|
118 |
test_create_signature_enveloping_hmac_sha512(); |
|
119 |
test_create_signature_enveloping_rsa(); |
|
120 |
test_create_signature_external_b64_dsa(); |
|
121 |
test_create_signature_external_dsa(); |
|
122 |
test_create_signature_keyname(); |
|
123 |
test_create_signature_retrievalmethod_rawx509crt(); |
|
124 |
test_create_signature_x509_crt_crl(); |
|
125 |
test_create_signature_x509_crt(); |
|
126 |
test_create_signature_x509_is(); |
|
127 |
test_create_signature_x509_ski(); |
|
128 |
test_create_signature_x509_sn(); |
|
4506
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
129 |
test_create_signature(); |
2 | 130 |
test_create_exc_signature(); |
131 |
test_create_sign_spec(); |
|
132 |
test_create_signature_enveloping_sha256_dsa(); |
|
133 |
test_create_signature_enveloping_sha384_rsa_sha256(); |
|
134 |
test_create_signature_enveloping_sha512_rsa_sha384(); |
|
135 |
test_create_signature_enveloping_sha512_rsa_sha512(); |
|
4506
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
136 |
test_create_signature_reference_dependency(); |
2 | 137 |
} |
138 |
||
139 |
private static void setup() throws Exception { |
|
140 |
fac = XMLSignatureFactory.getInstance(); |
|
141 |
kifac = fac.getKeyInfoFactory(); |
|
142 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
143 |
dbf.setNamespaceAware(true); |
|
144 |
db = dbf.newDocumentBuilder(); |
|
145 |
||
146 |
// get key & self-signed certificate from keystore |
|
147 |
FileInputStream fis = new FileInputStream(KEYSTORE); |
|
148 |
ks = KeyStore.getInstance("JKS"); |
|
149 |
ks.load(fis, "changeit".toCharArray()); |
|
150 |
signingKey = ks.getKey("user", "changeit".toCharArray()); |
|
151 |
signingCert = ks.getCertificate("user"); |
|
152 |
validatingKey = signingCert.getPublicKey(); |
|
153 |
||
154 |
// create common objects |
|
155 |
withoutComments = fac.newCanonicalizationMethod |
|
156 |
(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null); |
|
157 |
dsaSha1 = fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null); |
|
158 |
sha1 = fac.newDigestMethod(DigestMethod.SHA1, null); |
|
159 |
sha256 = fac.newDigestMethod(DigestMethod.SHA256, null); |
|
160 |
sha384 = fac.newDigestMethod |
|
161 |
("http://www.w3.org/2001/04/xmldsig-more#sha384", null); |
|
162 |
sha512 = fac.newDigestMethod(DigestMethod.SHA512, null); |
|
163 |
dsa = kifac.newKeyInfo(Collections.singletonList |
|
164 |
(kifac.newKeyValue(validatingKey))); |
|
165 |
rsa = kifac.newKeyInfo(Collections.singletonList |
|
166 |
(kifac.newKeyValue(getPublicKey("RSA")))); |
|
167 |
rsa1024 = kifac.newKeyInfo(Collections.singletonList |
|
168 |
(kifac.newKeyValue(getPublicKey("RSA", 1024)))); |
|
169 |
rsaSha1 = fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null); |
|
170 |
rsaSha256 = fac.newSignatureMethod |
|
171 |
("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", null); |
|
172 |
rsaSha384 = fac.newSignatureMethod |
|
173 |
("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384", null); |
|
174 |
rsaSha512 = fac.newSignatureMethod |
|
175 |
("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512", null); |
|
176 |
sks = new KeySelectors.SecretKeySelector("secret".getBytes("ASCII")); |
|
177 |
||
178 |
httpUd = new HttpURIDereferencer(); |
|
179 |
} |
|
180 |
||
181 |
static void test_create_signature_enveloped_dsa() throws Exception { |
|
182 |
System.out.println("* Generating signature-enveloped-dsa.xml"); |
|
183 |
// create SignedInfo |
|
184 |
SignedInfo si = fac.newSignedInfo |
|
185 |
(withoutComments, dsaSha1, Collections.singletonList |
|
186 |
(fac.newReference |
|
187 |
("", sha1, Collections.singletonList |
|
188 |
(fac.newTransform(Transform.ENVELOPED, |
|
189 |
(TransformParameterSpec) null)), |
|
190 |
null, null))); |
|
191 |
||
192 |
// create XMLSignature |
|
193 |
XMLSignature sig = fac.newXMLSignature(si, dsa); |
|
194 |
||
195 |
Document doc = db.newDocument(); |
|
196 |
Element envelope = doc.createElementNS |
|
197 |
("http://example.org/envelope", "Envelope"); |
|
198 |
envelope.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, |
|
199 |
"xmlns", "http://example.org/envelope"); |
|
200 |
doc.appendChild(envelope); |
|
201 |
||
202 |
DOMSignContext dsc = new DOMSignContext(signingKey, envelope); |
|
203 |
||
204 |
sig.sign(dsc); |
|
1337 | 205 |
// StringWriter sw = new StringWriter(); |
206 |
// dumpDocument(doc, sw); |
|
207 |
// System.out.println(sw.toString()); |
|
2 | 208 |
|
209 |
DOMValidateContext dvc = new DOMValidateContext |
|
210 |
(kvks, envelope.getFirstChild()); |
|
211 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
|
212 |
||
213 |
if (sig.equals(sig2) == false) { |
|
214 |
throw new Exception |
|
215 |
("Unmarshalled signature is not equal to generated signature"); |
|
216 |
} |
|
217 |
||
218 |
if (sig2.validate(dvc) == false) { |
|
219 |
throw new Exception("Validation of generated signature failed"); |
|
220 |
} |
|
221 |
System.out.println(); |
|
222 |
} |
|
223 |
||
224 |
static void test_create_signature_enveloping_b64_dsa() throws Exception { |
|
225 |
System.out.println("* Generating signature-enveloping-b64-dsa.xml"); |
|
226 |
test_create_signature_enveloping |
|
227 |
(sha1, dsaSha1, dsa, signingKey, kvks, true); |
|
228 |
System.out.println(); |
|
229 |
} |
|
230 |
||
231 |
static void test_create_signature_enveloping_dsa() throws Exception { |
|
232 |
System.out.println("* Generating signature-enveloping-dsa.xml"); |
|
233 |
test_create_signature_enveloping |
|
234 |
(sha1, dsaSha1, dsa, signingKey, kvks, false); |
|
235 |
System.out.println(); |
|
236 |
} |
|
237 |
||
238 |
static void test_create_signature_enveloping_sha256_dsa() throws Exception { |
|
239 |
System.out.println("* Generating signature-enveloping-sha256-dsa.xml"); |
|
240 |
test_create_signature_enveloping |
|
241 |
(sha256, dsaSha1, dsa, signingKey, kvks, false); |
|
242 |
System.out.println(); |
|
243 |
} |
|
244 |
||
245 |
static void test_create_signature_enveloping_hmac_sha1_40() |
|
246 |
throws Exception { |
|
247 |
System.out.println("* Generating signature-enveloping-hmac-sha1-40.xml"); |
|
248 |
SignatureMethod hmacSha1 = fac.newSignatureMethod |
|
249 |
(SignatureMethod.HMAC_SHA1, new HMACParameterSpec(40)); |
|
3462 | 250 |
try { |
251 |
test_create_signature_enveloping(sha1, hmacSha1, null, |
|
252 |
getSecretKey("secret".getBytes("ASCII")), sks, false); |
|
253 |
} catch (Exception e) { |
|
254 |
if (!(e instanceof XMLSignatureException)) { |
|
255 |
throw e; |
|
256 |
} |
|
257 |
} |
|
2 | 258 |
System.out.println(); |
259 |
} |
|
260 |
||
261 |
static void test_create_signature_enveloping_hmac_sha256() |
|
262 |
throws Exception { |
|
263 |
System.out.println("* Generating signature-enveloping-hmac-sha256.xml"); |
|
264 |
SignatureMethod hmacSha256 = fac.newSignatureMethod |
|
265 |
("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", null); |
|
266 |
test_create_signature_enveloping(sha1, hmacSha256, null, |
|
267 |
getSecretKey("secret".getBytes("ASCII")), sks, false); |
|
268 |
System.out.println(); |
|
269 |
} |
|
270 |
||
271 |
static void test_create_signature_enveloping_hmac_sha384() |
|
272 |
throws Exception { |
|
273 |
System.out.println("* Generating signature-enveloping-hmac-sha384.xml"); |
|
274 |
SignatureMethod hmacSha384 = fac.newSignatureMethod |
|
275 |
("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", null); |
|
276 |
test_create_signature_enveloping(sha1, hmacSha384, null, |
|
277 |
getSecretKey("secret".getBytes("ASCII")), sks, false); |
|
278 |
System.out.println(); |
|
279 |
} |
|
280 |
||
281 |
static void test_create_signature_enveloping_hmac_sha512() |
|
282 |
throws Exception { |
|
283 |
System.out.println("* Generating signature-enveloping-hmac-sha512.xml"); |
|
284 |
SignatureMethod hmacSha512 = fac.newSignatureMethod |
|
285 |
("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", null); |
|
286 |
test_create_signature_enveloping(sha1, hmacSha512, null, |
|
287 |
getSecretKey("secret".getBytes("ASCII")), sks, false); |
|
288 |
System.out.println(); |
|
289 |
} |
|
290 |
||
291 |
static void test_create_signature_enveloping_rsa() throws Exception { |
|
292 |
System.out.println("* Generating signature-enveloping-rsa.xml"); |
|
293 |
test_create_signature_enveloping(sha1, rsaSha1, rsa, |
|
294 |
getPrivateKey("RSA"), kvks, false); |
|
295 |
System.out.println(); |
|
296 |
} |
|
297 |
||
298 |
static void test_create_signature_enveloping_sha384_rsa_sha256() |
|
299 |
throws Exception { |
|
300 |
System.out.println("* Generating signature-enveloping-sha384-rsa_sha256.xml"); |
|
301 |
test_create_signature_enveloping(sha384, rsaSha256, rsa, |
|
302 |
getPrivateKey("RSA"), kvks, false); |
|
303 |
System.out.println(); |
|
304 |
} |
|
305 |
||
306 |
static void test_create_signature_enveloping_sha512_rsa_sha384() |
|
307 |
throws Exception { |
|
308 |
System.out.println("* Generating signature-enveloping-sha512-rsa_sha384.xml"); |
|
309 |
test_create_signature_enveloping(sha512, rsaSha384, rsa1024, |
|
310 |
getPrivateKey("RSA", 1024), kvks, false); |
|
311 |
System.out.println(); |
|
312 |
} |
|
313 |
||
314 |
static void test_create_signature_enveloping_sha512_rsa_sha512() |
|
315 |
throws Exception { |
|
316 |
System.out.println("* Generating signature-enveloping-sha512-rsa_sha512.xml"); |
|
317 |
test_create_signature_enveloping(sha512, rsaSha512, rsa1024, |
|
318 |
getPrivateKey("RSA", 1024), kvks, false); |
|
319 |
System.out.println(); |
|
320 |
} |
|
321 |
||
322 |
static void test_create_signature_external_b64_dsa() throws Exception { |
|
323 |
System.out.println("* Generating signature-external-b64-dsa.xml"); |
|
324 |
test_create_signature_external(dsaSha1, dsa, signingKey, kvks, true); |
|
325 |
System.out.println(); |
|
326 |
} |
|
327 |
||
328 |
static void test_create_signature_external_dsa() throws Exception { |
|
329 |
System.out.println("* Generating signature-external-dsa.xml"); |
|
330 |
test_create_signature_external(dsaSha1, dsa, signingKey, kvks, false); |
|
331 |
System.out.println(); |
|
332 |
} |
|
333 |
||
334 |
static void test_create_signature_keyname() throws Exception { |
|
335 |
System.out.println("* Generating signature-keyname.xml"); |
|
336 |
KeyInfo kn = kifac.newKeyInfo(Collections.singletonList |
|
337 |
(kifac.newKeyName("user"))); |
|
338 |
test_create_signature_external(dsaSha1, kn, signingKey, |
|
339 |
new X509KeySelector(ks), false); |
|
340 |
System.out.println(); |
|
341 |
} |
|
342 |
||
343 |
static void test_create_signature_retrievalmethod_rawx509crt() |
|
344 |
throws Exception { |
|
345 |
System.out.println( |
|
346 |
"* Generating signature-retrievalmethod-rawx509crt.xml"); |
|
347 |
KeyInfo rm = kifac.newKeyInfo(Collections.singletonList |
|
348 |
(kifac.newRetrievalMethod |
|
349 |
("certs/user.crt", X509Data.RAW_X509_CERTIFICATE_TYPE, null))); |
|
350 |
test_create_signature_external(dsaSha1, rm, signingKey, |
|
351 |
new X509KeySelector(ks), false); |
|
352 |
System.out.println(); |
|
353 |
} |
|
354 |
||
355 |
static void test_create_signature_x509_crt_crl() throws Exception { |
|
356 |
System.out.println("* Generating signature-x509-crt-crl.xml"); |
|
357 |
List<Object> xds = new ArrayList<Object>(); |
|
358 |
CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
|
359 |
xds.add(signingCert); |
|
360 |
FileInputStream fis = new FileInputStream(CRL); |
|
361 |
X509CRL crl = (X509CRL) cf.generateCRL(fis); |
|
362 |
fis.close(); |
|
363 |
xds.add(crl); |
|
364 |
KeyInfo crt_crl = kifac.newKeyInfo(Collections.singletonList |
|
365 |
(kifac.newX509Data(xds))); |
|
366 |
||
367 |
test_create_signature_external(dsaSha1, crt_crl, signingKey, |
|
368 |
new X509KeySelector(ks), false); |
|
369 |
System.out.println(); |
|
370 |
} |
|
371 |
||
372 |
static void test_create_signature_x509_crt() throws Exception { |
|
373 |
System.out.println("* Generating signature-x509-crt.xml"); |
|
374 |
KeyInfo crt = kifac.newKeyInfo(Collections.singletonList |
|
375 |
(kifac.newX509Data(Collections.singletonList(signingCert)))); |
|
376 |
||
377 |
test_create_signature_external(dsaSha1, crt, signingKey, |
|
378 |
new X509KeySelector(ks), false); |
|
379 |
System.out.println(); |
|
380 |
} |
|
381 |
||
382 |
static void test_create_signature_x509_is() throws Exception { |
|
383 |
System.out.println("* Generating signature-x509-is.xml"); |
|
384 |
KeyInfo is = kifac.newKeyInfo(Collections.singletonList |
|
385 |
(kifac.newX509Data(Collections.singletonList |
|
386 |
(kifac.newX509IssuerSerial |
|
387 |
("CN=User", new BigInteger("45ef2729", 16)))))); |
|
388 |
test_create_signature_external(dsaSha1, is, signingKey, |
|
389 |
new X509KeySelector(ks), false); |
|
390 |
System.out.println(); |
|
391 |
} |
|
392 |
||
393 |
static void test_create_signature_x509_ski() throws Exception { |
|
394 |
System.out.println("* Generating signature-x509-ski.xml"); |
|
395 |
KeyInfo ski = kifac.newKeyInfo(Collections.singletonList |
|
396 |
(kifac.newX509Data(Collections.singletonList |
|
397 |
("keyid".getBytes("ASCII"))))); |
|
398 |
||
399 |
test_create_signature_external(dsaSha1, ski, signingKey, |
|
400 |
KeySelector.singletonKeySelector(validatingKey), false); |
|
401 |
System.out.println(); |
|
402 |
} |
|
403 |
||
404 |
static void test_create_signature_x509_sn() throws Exception { |
|
405 |
System.out.println("* Generating signature-x509-sn.xml"); |
|
406 |
KeyInfo sn = kifac.newKeyInfo(Collections.singletonList |
|
407 |
(kifac.newX509Data(Collections.singletonList("CN=User")))); |
|
408 |
||
409 |
test_create_signature_external(dsaSha1, sn, signingKey, |
|
410 |
new X509KeySelector(ks), false); |
|
411 |
System.out.println(); |
|
412 |
} |
|
413 |
||
4506
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
414 |
static void test_create_signature_reference_dependency() throws Exception { |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
415 |
System.out.println("* Generating signature-reference-dependency.xml"); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
416 |
// create references |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
417 |
List<Reference> refs = Collections.singletonList |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
418 |
(fac.newReference("#object-1", sha1)); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
419 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
420 |
// create SignedInfo |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
421 |
SignedInfo si = fac.newSignedInfo(withoutComments, rsaSha1, refs); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
422 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
423 |
// create objects |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
424 |
List<XMLStructure> objs = new ArrayList<XMLStructure>(); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
425 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
426 |
// Object 1 |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
427 |
List<Reference> manRefs = Collections.singletonList |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
428 |
(fac.newReference("#object-2", sha1)); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
429 |
objs.add(fac.newXMLObject(Collections.singletonList |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
430 |
(fac.newManifest(manRefs, "manifest-1")), "object-1", null, null)); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
431 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
432 |
// Object 2 |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
433 |
Document doc = db.newDocument(); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
434 |
Element nc = doc.createElementNS(null, "NonCommentandus"); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
435 |
nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", ""); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
436 |
nc.appendChild(doc.createComment(" Commentandum ")); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
437 |
objs.add(fac.newXMLObject(Collections.singletonList |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
438 |
(new DOMStructure(nc)), "object-2", null, null)); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
439 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
440 |
// create XMLSignature |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
441 |
XMLSignature sig = fac.newXMLSignature(si, rsa, objs, "signature", null); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
442 |
DOMSignContext dsc = new DOMSignContext(getPrivateKey("RSA"), doc); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
443 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
444 |
sig.sign(dsc); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
445 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
446 |
// dumpDocument(doc, new PrintWriter(System.out)); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
447 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
448 |
DOMValidateContext dvc = new DOMValidateContext |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
449 |
(kvks, doc.getDocumentElement()); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
450 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
451 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
452 |
if (sig.equals(sig2) == false) { |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
453 |
throw new Exception |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
454 |
("Unmarshalled signature is not equal to generated signature"); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
455 |
} |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
456 |
if (sig2.validate(dvc) == false) { |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
457 |
throw new Exception("Validation of generated signature failed"); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
458 |
} |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
459 |
|
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
460 |
System.out.println(); |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
461 |
} |
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
462 |
|
2 | 463 |
static void test_create_signature() throws Exception { |
464 |
System.out.println("* Generating signature.xml"); |
|
465 |
||
466 |
// create references |
|
467 |
List<Reference> refs = new ArrayList<Reference>(); |
|
468 |
||
469 |
// Reference 1 |
|
470 |
refs.add(fac.newReference(STYLESHEET, sha1)); |
|
471 |
||
472 |
// Reference 2 |
|
473 |
refs.add(fac.newReference |
|
474 |
(STYLESHEET_B64, |
|
475 |
sha1, Collections.singletonList |
|
476 |
(fac.newTransform(Transform.BASE64, |
|
477 |
(TransformParameterSpec) null)), null, null)); |
|
478 |
||
479 |
// Reference 3 |
|
480 |
refs.add(fac.newReference("#object-1", sha1, Collections.singletonList |
|
481 |
(fac.newTransform(Transform.XPATH, |
|
482 |
new XPathFilterParameterSpec("self::text()"))), |
|
483 |
XMLObject.TYPE, null)); |
|
484 |
||
485 |
// Reference 4 |
|
486 |
String expr = "\n" |
|
487 |
+ " ancestor-or-self::dsig:SignedInfo " + "\n" |
|
488 |
+ " and " + "\n" |
|
489 |
+ " count(ancestor-or-self::dsig:Reference | " + "\n" |
|
490 |
+ " here()/ancestor::dsig:Reference[1]) > " + "\n" |
|
491 |
+ " count(ancestor-or-self::dsig:Reference) " + "\n" |
|
492 |
+ " or " + "\n" |
|
493 |
+ " count(ancestor-or-self::node() | " + "\n" |
|
494 |
+ " id('notaries')) = " + "\n" |
|
495 |
+ " count(ancestor-or-self::node()) " + "\n"; |
|
496 |
||
497 |
XPathFilterParameterSpec xfp = new XPathFilterParameterSpec(expr, |
|
498 |
Collections.singletonMap("dsig", XMLSignature.XMLNS)); |
|
499 |
refs.add(fac.newReference("", sha1, Collections.singletonList |
|
500 |
(fac.newTransform(Transform.XPATH, xfp)), |
|
501 |
XMLObject.TYPE, null)); |
|
502 |
||
503 |
// Reference 5 |
|
504 |
refs.add(fac.newReference("#object-2", sha1, Collections.singletonList |
|
505 |
(fac.newTransform |
|
506 |
(Transform.BASE64, (TransformParameterSpec) null)), |
|
507 |
XMLObject.TYPE, null)); |
|
508 |
||
509 |
// Reference 6 |
|
510 |
refs.add(fac.newReference |
|
511 |
("#manifest-1", sha1, null, Manifest.TYPE, null)); |
|
512 |
||
513 |
// Reference 7 |
|
514 |
refs.add(fac.newReference("#signature-properties-1", sha1, null, |
|
515 |
SignatureProperties.TYPE, null)); |
|
516 |
||
517 |
// Reference 8 |
|
518 |
List<Transform> transforms = new ArrayList<Transform>(); |
|
519 |
transforms.add(fac.newTransform |
|
520 |
(Transform.ENVELOPED, (TransformParameterSpec) null)); |
|
521 |
refs.add(fac.newReference("", sha1, transforms, null, null)); |
|
522 |
||
523 |
// Reference 9 |
|
524 |
transforms.add(fac.newTransform |
|
525 |
(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, |
|
526 |
(TransformParameterSpec) null)); |
|
527 |
refs.add(fac.newReference("", sha1, transforms, null, null)); |
|
528 |
||
529 |
// Reference 10 |
|
530 |
Transform env = fac.newTransform |
|
531 |
(Transform.ENVELOPED, (TransformParameterSpec) null); |
|
532 |
refs.add(fac.newReference("#xpointer(/)", |
|
533 |
sha1, Collections.singletonList(env), null, null)); |
|
534 |
||
535 |
// Reference 11 |
|
536 |
transforms.clear(); |
|
537 |
transforms.add(fac.newTransform |
|
538 |
(Transform.ENVELOPED, (TransformParameterSpec) null)); |
|
539 |
transforms.add(fac.newTransform |
|
540 |
(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, |
|
541 |
(TransformParameterSpec) null)); |
|
542 |
refs.add(fac.newReference("#xpointer(/)", sha1, transforms, |
|
543 |
null, null)); |
|
544 |
||
545 |
// Reference 12 |
|
546 |
refs.add |
|
547 |
(fac.newReference("#object-3", sha1, null, XMLObject.TYPE, null)); |
|
548 |
||
549 |
// Reference 13 |
|
550 |
Transform withComments = fac.newTransform |
|
551 |
(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, |
|
552 |
(TransformParameterSpec) null); |
|
553 |
refs.add(fac.newReference("#object-3", sha1, |
|
554 |
Collections.singletonList(withComments), XMLObject.TYPE, null)); |
|
555 |
||
556 |
// Reference 14 |
|
557 |
refs.add(fac.newReference("#xpointer(id('object-3'))", sha1, null, |
|
558 |
XMLObject.TYPE, null)); |
|
559 |
||
560 |
// Reference 15 |
|
561 |
withComments = fac.newTransform |
|
562 |
(CanonicalizationMethod.INCLUSIVE_WITH_COMMENTS, |
|
563 |
(TransformParameterSpec) null); |
|
564 |
refs.add(fac.newReference("#xpointer(id('object-3'))", sha1, |
|
565 |
Collections.singletonList(withComments), XMLObject.TYPE, null)); |
|
566 |
||
567 |
// Reference 16 |
|
568 |
refs.add(fac.newReference("#reference-2", sha1)); |
|
569 |
||
570 |
// Reference 17 |
|
571 |
refs.add(fac.newReference("#manifest-reference-1", sha1, null, |
|
572 |
null, "reference-1")); |
|
573 |
||
574 |
// Reference 18 |
|
575 |
refs.add(fac.newReference("#reference-1", sha1, null, null, |
|
576 |
"reference-2")); |
|
577 |
||
578 |
// create SignedInfo |
|
579 |
SignedInfo si = fac.newSignedInfo(withoutComments, dsaSha1, refs); |
|
580 |
||
581 |
// create keyinfo |
|
582 |
XPathFilterParameterSpec xpf = new XPathFilterParameterSpec( |
|
583 |
"ancestor-or-self::dsig:X509Data", |
|
584 |
Collections.singletonMap("dsig", XMLSignature.XMLNS)); |
|
585 |
RetrievalMethod rm = kifac.newRetrievalMethod("#object-4", |
|
586 |
X509Data.TYPE, Collections.singletonList(fac.newTransform |
|
587 |
(Transform.XPATH, xpf))); |
|
588 |
KeyInfo ki = kifac.newKeyInfo(Collections.singletonList(rm), null); |
|
589 |
||
590 |
Document doc = db.newDocument(); |
|
591 |
||
592 |
// create objects |
|
593 |
List<XMLStructure> objs = new ArrayList<XMLStructure>(); |
|
594 |
||
595 |
// Object 1 |
|
596 |
objs.add(fac.newXMLObject(Collections.singletonList |
|
597 |
(new DOMStructure(doc.createTextNode("I am the text."))), |
|
598 |
"object-1", "text/plain", null)); |
|
599 |
||
600 |
// Object 2 |
|
601 |
objs.add(fac.newXMLObject(Collections.singletonList |
|
602 |
(new DOMStructure(doc.createTextNode("SSBhbSB0aGUgdGV4dC4="))), |
|
603 |
"object-2", "text/plain", Transform.BASE64)); |
|
604 |
||
605 |
// Object 3 |
|
606 |
Element nc = doc.createElementNS(null, "NonCommentandus"); |
|
607 |
nc.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", ""); |
|
608 |
nc.appendChild(doc.createComment(" Commentandum ")); |
|
609 |
objs.add(fac.newXMLObject(Collections.singletonList |
|
610 |
(new DOMStructure(nc)), "object-3", null, null)); |
|
611 |
||
612 |
// Manifest |
|
613 |
List<Reference> manRefs = new ArrayList<Reference>(); |
|
614 |
||
615 |
// Manifest Reference 1 |
|
616 |
manRefs.add(fac.newReference(STYLESHEET, |
|
617 |
sha1, null, null, "manifest-reference-1")); |
|
618 |
||
619 |
// Manifest Reference 2 |
|
620 |
manRefs.add(fac.newReference("#reference-1", sha1)); |
|
621 |
||
622 |
// Manifest Reference 3 |
|
623 |
List<Transform> manTrans = new ArrayList<Transform>(); |
|
624 |
String xslt = "" |
|
625 |
+ "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n" |
|
626 |
+ " xmlns='http://www.w3.org/TR/xhtml1/strict' \n" |
|
627 |
+ " exclude-result-prefixes='foo' \n" |
|
628 |
+ " version='1.0'>\n" |
|
629 |
+ " <xsl:output encoding='UTF-8' \n" |
|
630 |
+ " indent='no' \n" |
|
631 |
+ " method='xml' />\n" |
|
632 |
+ " <xsl:template match='/'>\n" |
|
633 |
+ " <html>\n" |
|
634 |
+ " <head>\n" |
|
635 |
+ " <title>Notaries</title>\n" |
|
636 |
+ " </head>\n" |
|
637 |
+ " <body>\n" |
|
638 |
+ " <table>\n" |
|
639 |
+ " <xsl:for-each select='Notaries/Notary'>\n" |
|
640 |
+ " <tr>\n" |
|
641 |
+ " <th>\n" |
|
642 |
+ " <xsl:value-of select='@name' />\n" |
|
643 |
+ " </th>\n" |
|
644 |
+ " </tr>\n" |
|
645 |
+ " </xsl:for-each>\n" |
|
646 |
+ " </table>\n" |
|
647 |
+ " </body>\n" |
|
648 |
+ " </html>\n" |
|
649 |
+ " </xsl:template>\n" |
|
650 |
+ "</xsl:stylesheet>\n"; |
|
651 |
Document docxslt = db.parse(new ByteArrayInputStream(xslt.getBytes())); |
|
652 |
Node xslElem = docxslt.getDocumentElement(); |
|
653 |
||
654 |
manTrans.add(fac.newTransform(Transform.XSLT, |
|
655 |
new XSLTTransformParameterSpec(new DOMStructure(xslElem)))); |
|
656 |
manTrans.add(fac.newTransform(CanonicalizationMethod.INCLUSIVE, |
|
657 |
(TransformParameterSpec) null)); |
|
658 |
manRefs.add(fac.newReference("#notaries", sha1, manTrans, null, null)); |
|
659 |
||
660 |
objs.add(fac.newXMLObject(Collections.singletonList |
|
661 |
(fac.newManifest(manRefs, "manifest-1")), null, null, null)); |
|
662 |
||
663 |
// SignatureProperties |
|
664 |
Element sa = doc.createElementNS("urn:demo", "SignerAddress"); |
|
665 |
sa.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:demo"); |
|
666 |
Element ip = doc.createElementNS("urn:demo", "IP"); |
|
667 |
ip.appendChild(doc.createTextNode("192.168.21.138")); |
|
668 |
sa.appendChild(ip); |
|
669 |
SignatureProperty sp = fac.newSignatureProperty |
|
670 |
(Collections.singletonList(new DOMStructure(sa)), |
|
671 |
"#signature", null); |
|
672 |
SignatureProperties sps = fac.newSignatureProperties |
|
673 |
(Collections.singletonList(sp), "signature-properties-1"); |
|
674 |
objs.add(fac.newXMLObject(Collections.singletonList(sps), null, |
|
675 |
null, null)); |
|
676 |
||
677 |
// Object 4 |
|
678 |
List<Object> xds = new ArrayList<Object>(); |
|
679 |
xds.add("CN=User"); |
|
680 |
xds.add(kifac.newX509IssuerSerial |
|
681 |
("CN=User", new BigInteger("45ef2729", 16))); |
|
682 |
xds.add(signingCert); |
|
683 |
objs.add(fac.newXMLObject(Collections.singletonList |
|
684 |
(kifac.newX509Data(xds)), "object-4", null, null)); |
|
685 |
||
686 |
// create XMLSignature |
|
687 |
XMLSignature sig = fac.newXMLSignature(si, ki, objs, "signature", null); |
|
688 |
||
689 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
690 |
dbf.setNamespaceAware(true); |
|
691 |
dbf.setValidating(false); |
|
692 |
Document envDoc = dbf.newDocumentBuilder().parse |
|
693 |
(new FileInputStream(ENVELOPE)); |
|
694 |
Element ys = (Element) |
|
695 |
envDoc.getElementsByTagName("YoursSincerely").item(0); |
|
696 |
||
697 |
DOMSignContext dsc = new DOMSignContext(signingKey, ys); |
|
4506
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
698 |
dsc.setURIDereferencer(httpUd); |
2 | 699 |
|
700 |
sig.sign(dsc); |
|
701 |
||
702 |
// StringWriter sw = new StringWriter(); |
|
703 |
// dumpDocument(envDoc, sw); |
|
704 |
||
705 |
NodeList nl = |
|
706 |
envDoc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature"); |
|
707 |
if (nl.getLength() == 0) { |
|
708 |
throw new Exception("Couldn't find signature Element"); |
|
709 |
} |
|
710 |
Element sigElement = (Element) nl.item(0); |
|
711 |
||
712 |
DOMValidateContext dvc = new DOMValidateContext |
|
713 |
(new X509KeySelector(ks), sigElement); |
|
4506
402b248b41de
6867348: Digest Value of References inside Manifest - calculation order problem
mullan
parents:
3462
diff
changeset
|
714 |
dvc.setURIDereferencer(httpUd); |
2 | 715 |
File f = new File( |
716 |
System.getProperty("dir.test.vector.baltimore") + |
|
717 |
System.getProperty("file.separator") + |
|
718 |
"merlin-xmldsig-twenty-three" + |
|
719 |
System.getProperty("file.separator")); |
|
720 |
dvc.setBaseURI(f.toURI().toString()); |
|
721 |
||
722 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
|
723 |
||
724 |
if (sig.equals(sig2) == false) { |
|
725 |
throw new Exception |
|
726 |
("Unmarshalled signature is not equal to generated signature"); |
|
727 |
} |
|
728 |
if (sig2.validate(dvc) == false) { |
|
729 |
throw new Exception("Validation of generated signature failed"); |
|
730 |
} |
|
731 |
System.out.println(); |
|
732 |
} |
|
733 |
||
734 |
private static void dumpDocument(Document doc, Writer w) throws Exception { |
|
735 |
TransformerFactory tf = TransformerFactory.newInstance(); |
|
736 |
Transformer trans = tf.newTransformer(); |
|
737 |
// trans.setOutputProperty(OutputKeys.INDENT, "yes"); |
|
738 |
trans.transform(new DOMSource(doc), new StreamResult(w)); |
|
739 |
} |
|
740 |
||
741 |
private static void test_create_signature_external |
|
742 |
(SignatureMethod sm, KeyInfo ki, Key signingKey, KeySelector ks, |
|
743 |
boolean b64) throws Exception { |
|
744 |
||
745 |
// create reference |
|
746 |
Reference ref; |
|
747 |
if (b64) { |
|
748 |
ref = fac.newReference |
|
749 |
(STYLESHEET_B64, |
|
750 |
sha1, Collections.singletonList |
|
751 |
(fac.newTransform(Transform.BASE64, |
|
752 |
(TransformParameterSpec) null)), null, null); |
|
753 |
} else { |
|
754 |
ref = fac.newReference(STYLESHEET, sha1); |
|
755 |
} |
|
756 |
||
757 |
// create SignedInfo |
|
758 |
SignedInfo si = fac.newSignedInfo(withoutComments, sm, |
|
759 |
Collections.singletonList(ref)); |
|
760 |
||
761 |
Document doc = db.newDocument(); |
|
762 |
||
763 |
// create XMLSignature |
|
764 |
XMLSignature sig = fac.newXMLSignature(si, ki); |
|
765 |
||
766 |
DOMSignContext dsc = new DOMSignContext(signingKey, doc); |
|
767 |
dsc.setURIDereferencer(httpUd); |
|
768 |
||
769 |
sig.sign(dsc); |
|
770 |
||
771 |
DOMValidateContext dvc = new DOMValidateContext |
|
772 |
(ks, doc.getDocumentElement()); |
|
773 |
File f = new File(DATA_DIR); |
|
774 |
dvc.setBaseURI(f.toURI().toString()); |
|
775 |
dvc.setURIDereferencer(httpUd); |
|
776 |
||
777 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
|
778 |
||
779 |
if (sig.equals(sig2) == false) { |
|
780 |
throw new Exception |
|
781 |
("Unmarshalled signature is not equal to generated signature"); |
|
782 |
} |
|
783 |
if (sig2.validate(dvc) == false) { |
|
784 |
throw new Exception("Validation of generated signature failed"); |
|
785 |
} |
|
786 |
} |
|
787 |
||
788 |
private static void test_create_signature_enveloping |
|
789 |
(DigestMethod dm, SignatureMethod sm, KeyInfo ki, Key signingKey, |
|
790 |
KeySelector ks, boolean b64) throws Exception { |
|
791 |
||
792 |
// create reference |
|
793 |
Reference ref; |
|
794 |
if (b64) { |
|
795 |
ref = fac.newReference("#object", dm, Collections.singletonList |
|
796 |
(fac.newTransform(Transform.BASE64, |
|
797 |
(TransformParameterSpec) null)), null, null); |
|
798 |
} else { |
|
799 |
ref = fac.newReference("#object", dm); |
|
800 |
} |
|
801 |
||
802 |
// create SignedInfo |
|
803 |
SignedInfo si = fac.newSignedInfo(withoutComments, sm, |
|
804 |
Collections.singletonList(ref)); |
|
805 |
||
806 |
Document doc = db.newDocument(); |
|
807 |
// create Objects |
|
808 |
String text = b64 ? "c29tZSB0ZXh0" : "some text"; |
|
809 |
XMLObject obj = fac.newXMLObject(Collections.singletonList |
|
810 |
(new DOMStructure(doc.createTextNode(text))), |
|
811 |
"object", null, null); |
|
812 |
||
813 |
// create XMLSignature |
|
814 |
XMLSignature sig = fac.newXMLSignature |
|
815 |
(si, ki, Collections.singletonList(obj), null, null); |
|
816 |
||
817 |
DOMSignContext dsc = new DOMSignContext(signingKey, doc); |
|
818 |
||
819 |
sig.sign(dsc); |
|
820 |
||
821 |
// dumpDocument(doc, new FileWriter("/tmp/foo.xml")); |
|
822 |
||
823 |
DOMValidateContext dvc = new DOMValidateContext |
|
824 |
(ks, doc.getDocumentElement()); |
|
825 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
|
826 |
||
827 |
if (sig.equals(sig2) == false) { |
|
828 |
throw new Exception |
|
829 |
("Unmarshalled signature is not equal to generated signature"); |
|
830 |
} |
|
831 |
if (sig2.validate(dvc) == false) { |
|
832 |
throw new Exception("Validation of generated signature failed"); |
|
833 |
} |
|
834 |
} |
|
835 |
||
836 |
static void test_create_exc_signature() throws Exception { |
|
837 |
System.out.println("* Generating exc_signature.xml"); |
|
838 |
List<Reference> refs = new ArrayList<Reference>(4); |
|
839 |
||
840 |
// create reference 1 |
|
841 |
refs.add(fac.newReference |
|
842 |
("#xpointer(id('to-be-signed'))", |
|
843 |
fac.newDigestMethod(DigestMethod.SHA1, null), |
|
844 |
Collections.singletonList |
|
845 |
(fac.newTransform(CanonicalizationMethod.EXCLUSIVE, |
|
846 |
(TransformParameterSpec) null)), |
|
847 |
null, null)); |
|
848 |
||
849 |
// create reference 2 |
|
850 |
List<String> prefixList = new ArrayList<String>(2); |
|
851 |
prefixList.add("bar"); |
|
852 |
prefixList.add("#default"); |
|
853 |
ExcC14NParameterSpec params = new ExcC14NParameterSpec(prefixList); |
|
854 |
refs.add(fac.newReference |
|
855 |
("#xpointer(id('to-be-signed'))", |
|
856 |
fac.newDigestMethod(DigestMethod.SHA1, null), |
|
857 |
Collections.singletonList |
|
858 |
(fac.newTransform(CanonicalizationMethod.EXCLUSIVE, params)), |
|
859 |
null, null)); |
|
860 |
||
861 |
// create reference 3 |
|
862 |
refs.add(fac.newReference |
|
863 |
("#xpointer(id('to-be-signed'))", |
|
864 |
fac.newDigestMethod(DigestMethod.SHA1, null), |
|
865 |
Collections.singletonList(fac.newTransform |
|
866 |
(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, |
|
867 |
(TransformParameterSpec) null)), |
|
868 |
null, null)); |
|
869 |
||
870 |
// create reference 4 |
|
871 |
prefixList = new ArrayList<String>(2); |
|
872 |
prefixList.add("bar"); |
|
873 |
prefixList.add("#default"); |
|
874 |
params = new ExcC14NParameterSpec(prefixList); |
|
875 |
refs.add(fac.newReference |
|
876 |
("#xpointer(id('to-be-signed'))", |
|
877 |
fac.newDigestMethod(DigestMethod.SHA1, null), |
|
878 |
Collections.singletonList(fac.newTransform |
|
879 |
(CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS, params)), |
|
880 |
null, null)); |
|
881 |
||
882 |
// create SignedInfo |
|
883 |
SignedInfo si = fac.newSignedInfo( |
|
884 |
fac.newCanonicalizationMethod |
|
885 |
(CanonicalizationMethod.EXCLUSIVE, |
|
886 |
(C14NMethodParameterSpec) null), |
|
887 |
fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); |
|
888 |
||
889 |
// create KeyInfo |
|
890 |
List<XMLStructure> kits = new ArrayList<XMLStructure>(2); |
|
891 |
kits.add(kifac.newKeyValue(validatingKey)); |
|
892 |
KeyInfo ki = kifac.newKeyInfo(kits); |
|
893 |
||
894 |
// create Objects |
|
895 |
Document doc = db.newDocument(); |
|
896 |
Element baz = doc.createElementNS("urn:bar", "bar:Baz"); |
|
897 |
Comment com = doc.createComment(" comment "); |
|
898 |
baz.appendChild(com); |
|
899 |
XMLObject obj = fac.newXMLObject(Collections.singletonList |
|
900 |
(new DOMStructure(baz)), "to-be-signed", null, null); |
|
901 |
||
902 |
// create XMLSignature |
|
903 |
XMLSignature sig = fac.newXMLSignature |
|
904 |
(si, ki, Collections.singletonList(obj), null, null); |
|
905 |
||
906 |
Element foo = doc.createElementNS("urn:foo", "Foo"); |
|
907 |
foo.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:foo"); |
|
908 |
foo.setAttributeNS |
|
909 |
("http://www.w3.org/2000/xmlns/", "xmlns:bar", "urn:bar"); |
|
910 |
doc.appendChild(foo); |
|
911 |
||
912 |
DOMSignContext dsc = new DOMSignContext(signingKey, foo); |
|
913 |
dsc.putNamespacePrefix(XMLSignature.XMLNS, "dsig"); |
|
914 |
||
915 |
sig.sign(dsc); |
|
916 |
||
917 |
// dumpDocument(doc, new FileWriter("/tmp/foo.xml")); |
|
918 |
||
919 |
DOMValidateContext dvc = new DOMValidateContext |
|
920 |
(new KeySelectors.KeyValueKeySelector(), foo.getLastChild()); |
|
921 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
|
922 |
||
923 |
if (sig.equals(sig2) == false) { |
|
924 |
throw new Exception |
|
925 |
("Unmarshalled signature is not equal to generated signature"); |
|
926 |
} |
|
927 |
if (sig2.validate(dvc) == false) { |
|
928 |
throw new Exception("Validation of generated signature failed"); |
|
929 |
} |
|
930 |
System.out.println(); |
|
931 |
} |
|
932 |
||
933 |
static void test_create_sign_spec() throws Exception { |
|
934 |
System.out.println("* Generating sign-spec.xml"); |
|
935 |
List<Reference> refs = new ArrayList<Reference>(2); |
|
936 |
||
937 |
// create reference 1 |
|
938 |
List<XPathType> types = new ArrayList<XPathType>(3); |
|
939 |
types.add(new XPathType(" //ToBeSigned ", XPathType.Filter.INTERSECT)); |
|
940 |
types.add(new XPathType(" //NotToBeSigned ", |
|
941 |
XPathType.Filter.SUBTRACT)); |
|
942 |
types.add(new XPathType(" //ReallyToBeSigned ", |
|
943 |
XPathType.Filter.UNION)); |
|
944 |
XPathFilter2ParameterSpec xp1 = new XPathFilter2ParameterSpec(types); |
|
945 |
refs.add(fac.newReference |
|
946 |
("", fac.newDigestMethod(DigestMethod.SHA1, null), |
|
947 |
Collections.singletonList(fac.newTransform(Transform.XPATH2, xp1)), |
|
948 |
null, null)); |
|
949 |
||
950 |
// create reference 2 |
|
951 |
List<Transform> trans2 = new ArrayList<Transform>(2); |
|
952 |
trans2.add(fac.newTransform(Transform.ENVELOPED, |
|
953 |
(TransformParameterSpec) null)); |
|
954 |
XPathFilter2ParameterSpec xp2 = new XPathFilter2ParameterSpec |
|
955 |
(Collections.singletonList |
|
956 |
(new XPathType(" / ", XPathType.Filter.UNION))); |
|
957 |
trans2.add(fac.newTransform(Transform.XPATH2, xp2)); |
|
958 |
refs.add(fac.newReference("#signature-value", |
|
959 |
fac.newDigestMethod(DigestMethod.SHA1, null), trans2, null, null)); |
|
960 |
||
961 |
// create SignedInfo |
|
962 |
SignedInfo si = fac.newSignedInfo( |
|
963 |
fac.newCanonicalizationMethod |
|
964 |
(CanonicalizationMethod.INCLUSIVE, |
|
965 |
(C14NMethodParameterSpec) null), |
|
966 |
fac.newSignatureMethod(SignatureMethod.DSA_SHA1, null), refs); |
|
967 |
||
968 |
// create KeyInfo |
|
969 |
List<XMLStructure> kits = new ArrayList<XMLStructure>(2); |
|
970 |
kits.add(kifac.newKeyValue(validatingKey)); |
|
971 |
List<Object> xds = new ArrayList<Object>(2); |
|
972 |
xds.add("CN=User"); |
|
973 |
xds.add(signingCert); |
|
974 |
kits.add(kifac.newX509Data(xds)); |
|
975 |
KeyInfo ki = kifac.newKeyInfo(kits); |
|
976 |
||
977 |
// create XMLSignature |
|
978 |
XMLSignature sig = fac.newXMLSignature |
|
979 |
(si, ki, null, null, "signature-value"); |
|
980 |
||
981 |
Document doc = db.newDocument(); |
|
982 |
Element tbs1 = doc.createElementNS(null, "ToBeSigned"); |
|
983 |
Comment tbs1Com = doc.createComment(" comment "); |
|
984 |
Element tbs1Data = doc.createElementNS(null, "Data"); |
|
985 |
Element tbs1ntbs = doc.createElementNS(null, "NotToBeSigned"); |
|
986 |
Element tbs1rtbs = doc.createElementNS(null, "ReallyToBeSigned"); |
|
987 |
Comment tbs1rtbsCom = doc.createComment(" comment "); |
|
988 |
Element tbs1rtbsData = doc.createElementNS(null, "Data"); |
|
989 |
tbs1rtbs.appendChild(tbs1rtbsCom); |
|
990 |
tbs1rtbs.appendChild(tbs1rtbsData); |
|
991 |
tbs1ntbs.appendChild(tbs1rtbs); |
|
992 |
tbs1.appendChild(tbs1Com); |
|
993 |
tbs1.appendChild(tbs1Data); |
|
994 |
tbs1.appendChild(tbs1ntbs); |
|
995 |
||
996 |
Element tbs2 = doc.createElementNS(null, "ToBeSigned"); |
|
997 |
Element tbs2Data = doc.createElementNS(null, "Data"); |
|
998 |
Element tbs2ntbs = doc.createElementNS(null, "NotToBeSigned"); |
|
999 |
Element tbs2ntbsData = doc.createElementNS(null, "Data"); |
|
1000 |
tbs2ntbs.appendChild(tbs2ntbsData); |
|
1001 |
tbs2.appendChild(tbs2Data); |
|
1002 |
tbs2.appendChild(tbs2ntbs); |
|
1003 |
||
1004 |
Element document = doc.createElementNS(null, "Document"); |
|
1005 |
document.appendChild(tbs1); |
|
1006 |
document.appendChild(tbs2); |
|
1007 |
doc.appendChild(document); |
|
1008 |
||
1009 |
DOMSignContext dsc = new DOMSignContext(signingKey, document); |
|
1010 |
||
1011 |
sig.sign(dsc); |
|
1012 |
||
1013 |
// dumpDocument(doc, new FileWriter("/tmp/foo.xml")); |
|
1014 |
||
1015 |
DOMValidateContext dvc = new DOMValidateContext |
|
1016 |
(new KeySelectors.KeyValueKeySelector(), document.getLastChild()); |
|
1017 |
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc); |
|
1018 |
||
1019 |
if (sig.equals(sig2) == false) { |
|
1020 |
throw new Exception |
|
1021 |
("Unmarshalled signature is not equal to generated signature"); |
|
1022 |
} |
|
1023 |
if (sig2.validate(dvc) == false) { |
|
1024 |
throw new Exception("Validation of generated signature failed"); |
|
1025 |
} |
|
1026 |
System.out.println(); |
|
1027 |
} |
|
1028 |
||
1029 |
private static final String DSA_Y = |
|
1030 |
"070662842167565771936588335128634396171789331656318483584455493822" + |
|
1031 |
"400811200853331373030669235424928346190274044631949560438023934623" + |
|
1032 |
"71310375123430985057160"; |
|
1033 |
private static final String DSA_P = |
|
1034 |
"013232376895198612407547930718267435757728527029623408872245156039" + |
|
1035 |
"757713029036368719146452186041204237350521785240337048752071462798" + |
|
1036 |
"273003935646236777459223"; |
|
1037 |
private static final String DSA_Q = |
|
1038 |
"0857393771208094202104259627990318636601332086981"; |
|
1039 |
private static final String DSA_G = |
|
1040 |
"054216440574364751416096484883257051280474283943804743768346673007" + |
|
1041 |
"661082626139005426812890807137245973106730741193551360857959820973" + |
|
1042 |
"90670890367185141189796"; |
|
1043 |
private static final String DSA_X = |
|
1044 |
"0527140396812450214498055937934275626078768840117"; |
|
1045 |
private static final String RSA_MOD = |
|
1046 |
"010800185049102889923150759252557522305032794699952150943573164381" + |
|
1047 |
"936603255999071981574575044810461362008102247767482738822150129277" + |
|
1048 |
"490998033971789476107463"; |
|
1049 |
private static final String RSA_PRIV = |
|
1050 |
"016116973584421969795445996229612671947635798429212816611707210835" + |
|
1051 |
"915586591340598683996088487065438751488342251960069575392056288063" + |
|
1052 |
"6800379454345804879553"; |
|
1053 |
private static final String RSA_PUB = "065537"; |
|
1054 |
private static final String RSA_1024_MOD = "098871307553789439961130765" + |
|
1055 |
"909423744508062468450669519128736624058048856940468016843888594585" + |
|
1056 |
"322862378444314635412341974900625010364163960238734457710620107530" + |
|
1057 |
"573945081856371709138380902553309075505688814637544923038853658690" + |
|
1058 |
"857672483016239697038853418682988686871489963827000080098971762923" + |
|
1059 |
"833614557257607521"; |
|
1060 |
private static final String RSA_1024_PRIV = "03682574144968491431483287" + |
|
1061 |
"297021581096848810374110568017963075809477047466189822987258068867" + |
|
1062 |
"704855380407747867998863645890602646601140183818953428006646987710" + |
|
1063 |
"237008997971129772408397621801631622129297063463868593083106979716" + |
|
1064 |
"204903524890556839550490384015324575598723478554854070823335021842" + |
|
1065 |
"210112348400928769"; |
|
1066 |
||
1067 |
private static PublicKey getPublicKey(String algo) throws Exception { |
|
1068 |
return getPublicKey(algo, 512); |
|
1069 |
} |
|
1070 |
||
1071 |
private static PublicKey getPublicKey(String algo, int keysize) |
|
1072 |
throws Exception { |
|
1073 |
KeyFactory kf = KeyFactory.getInstance(algo); |
|
1074 |
KeySpec kspec; |
|
1075 |
if (algo.equalsIgnoreCase("DSA")) { |
|
1076 |
kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y), |
|
1077 |
new BigInteger(DSA_P), |
|
1078 |
new BigInteger(DSA_Q), |
|
1079 |
new BigInteger(DSA_G)); |
|
1080 |
} else if (algo.equalsIgnoreCase("RSA")) { |
|
1081 |
if (keysize == 512) { |
|
1082 |
kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD), |
|
1083 |
new BigInteger(RSA_PUB)); |
|
1084 |
} else { |
|
1085 |
kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD), |
|
1086 |
new BigInteger(RSA_PUB)); |
|
1087 |
} |
|
1088 |
} else throw new RuntimeException("Unsupported key algorithm " + algo); |
|
1089 |
return kf.generatePublic(kspec); |
|
1090 |
} |
|
1091 |
||
1092 |
private static PrivateKey getPrivateKey(String algo) throws Exception { |
|
1093 |
return getPrivateKey(algo, 512); |
|
1094 |
} |
|
1095 |
||
1096 |
private static PrivateKey getPrivateKey(String algo, int keysize) |
|
1097 |
throws Exception { |
|
1098 |
KeyFactory kf = KeyFactory.getInstance(algo); |
|
1099 |
KeySpec kspec; |
|
1100 |
if (algo.equalsIgnoreCase("DSA")) { |
|
1101 |
kspec = new DSAPrivateKeySpec |
|
1102 |
(new BigInteger(DSA_X), new BigInteger(DSA_P), |
|
1103 |
new BigInteger(DSA_Q), new BigInteger(DSA_G)); |
|
1104 |
} else if (algo.equalsIgnoreCase("RSA")) { |
|
1105 |
if (keysize == 512) { |
|
1106 |
kspec = new RSAPrivateKeySpec |
|
1107 |
(new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV)); |
|
1108 |
} else { |
|
1109 |
kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD), |
|
1110 |
new BigInteger(RSA_1024_PRIV)); |
|
1111 |
} |
|
1112 |
} else throw new RuntimeException("Unsupported key algorithm " + algo); |
|
1113 |
return kf.generatePrivate(kspec); |
|
1114 |
} |
|
1115 |
||
1116 |
private static SecretKey getSecretKey(final byte[] secret) { |
|
1117 |
return new SecretKey() { |
|
1118 |
public String getFormat() { return "RAW"; } |
|
1119 |
public byte[] getEncoded() { return secret; } |
|
1120 |
public String getAlgorithm(){ return "SECRET"; } |
|
1121 |
}; |
|
1122 |
} |
|
1123 |
||
1124 |
/** |
|
1125 |
* This URIDereferencer returns locally cached copies of http content to |
|
1126 |
* avoid test failures due to network glitches, etc. |
|
1127 |
*/ |
|
1128 |
private static class HttpURIDereferencer implements URIDereferencer { |
|
1129 |
private URIDereferencer defaultUd; |
|
1130 |
||
1131 |
HttpURIDereferencer() { |
|
1132 |
defaultUd = XMLSignatureFactory.getInstance().getURIDereferencer(); |
|
1133 |
} |
|
1134 |
||
1135 |
public Data dereference(final URIReference ref, XMLCryptoContext ctx) |
|
1136 |
throws URIReferenceException { |
|
1137 |
String uri = ref.getURI(); |
|
1138 |
if (uri.equals(STYLESHEET) || uri.equals(STYLESHEET_B64)) { |
|
1139 |
try { |
|
1140 |
FileInputStream fis = new FileInputStream(new File |
|
1141 |
(DATA_DIR, uri.substring(uri.lastIndexOf('/')))); |
|
1142 |
return new OctetStreamData(fis,ref.getURI(),ref.getType()); |
|
1143 |
} catch (Exception e) { throw new URIReferenceException(e); } |
|
1144 |
} |
|
1145 |
||
1146 |
// fallback on builtin deref |
|
1147 |
return defaultUd.dereference(ref, ctx); |
|
1148 |
} |
|
1149 |
} |
|
1150 |
} |