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