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