author | jwilhelm |
Thu, 04 Jun 2015 14:19:51 +0200 | |
changeset 31034 | 6d34754bd5e0 |
parent 23010 | 6dadb192ad81 |
child 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
3462 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18266
diff
changeset
|
2 |
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. |
3462 | 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. |
|
3462 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test %I% %E% |
|
3466
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
26 |
* @bug 6824440 6858484 |
3462 | 27 |
* @summary Check that Apache XMLSec APIs will not accept HMAC truncation |
28 |
* lengths less than minimum bound |
|
29 |
* @compile -XDignore.symbol.file TruncateHMAC.java |
|
30 |
* @run main TruncateHMAC |
|
31 |
*/ |
|
32 |
||
33 |
import java.io.File; |
|
34 |
import javax.crypto.SecretKey; |
|
35 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
36 |
import org.w3c.dom.Document; |
|
37 |
import org.w3c.dom.Element; |
|
38 |
import org.w3c.dom.NodeList; |
|
39 |
||
40 |
import com.sun.org.apache.xml.internal.security.Init; |
|
41 |
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; |
|
42 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignature; |
|
43 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; |
|
44 |
import com.sun.org.apache.xml.internal.security.utils.Constants; |
|
45 |
||
46 |
||
47 |
public class TruncateHMAC { |
|
48 |
||
49 |
private final static String DIR = System.getProperty("test.src", "."); |
|
50 |
private static DocumentBuilderFactory dbf = null; |
|
51 |
private static boolean atLeastOneFailed = false; |
|
52 |
||
53 |
public static void main(String[] args) throws Exception { |
|
54 |
||
55 |
Init.init(); |
|
56 |
dbf = DocumentBuilderFactory.newInstance(); |
|
57 |
dbf.setNamespaceAware(true); |
|
58 |
dbf.setValidating(false); |
|
3466
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
59 |
validate("signature-enveloping-hmac-sha1-trunclen-0-attack.xml", false); |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
60 |
validate("signature-enveloping-hmac-sha1-trunclen-8-attack.xml", false); |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
61 |
// this one should pass |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
62 |
validate("signature-enveloping-hmac-sha1.xml", true); |
3462 | 63 |
generate_hmac_sha1_40(); |
64 |
||
65 |
if (atLeastOneFailed) { |
|
66 |
throw new Exception |
|
67 |
("At least one signature did not validate as expected"); |
|
68 |
} |
|
69 |
} |
|
70 |
||
3466
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
71 |
private static void validate(String data, boolean pass) throws Exception { |
3462 | 72 |
System.out.println("Validating " + data); |
73 |
File file = new File(DIR, data); |
|
74 |
||
75 |
Document doc = dbf.newDocumentBuilder().parse(file); |
|
76 |
NodeList nl = |
|
77 |
doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "Signature"); |
|
78 |
if (nl.getLength() == 0) { |
|
79 |
throw new Exception("Couldn't find signature Element"); |
|
80 |
} |
|
81 |
Element sigElement = (Element) nl.item(0); |
|
82 |
XMLSignature signature = new XMLSignature |
|
83 |
(sigElement, file.toURI().toString()); |
|
84 |
SecretKey sk = signature.createSecretKey("secret".getBytes("ASCII")); |
|
85 |
try { |
|
86 |
System.out.println |
|
87 |
("Validation status: " + signature.checkSignatureValue(sk)); |
|
3466
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
88 |
if (!pass) { |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
89 |
System.out.println("FAILED"); |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
90 |
atLeastOneFailed = true; |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
91 |
} else { |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
92 |
System.out.println("PASSED"); |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
93 |
} |
3462 | 94 |
} catch (XMLSignatureException xse) { |
95 |
System.out.println(xse.getMessage()); |
|
3466
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
96 |
if (!pass) { |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
97 |
System.out.println("PASSED"); |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
98 |
} else { |
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
99 |
System.out.println("FAILED"); |
18266
26e69da689b9
8010714: XML DSig API allows a RetrievalMethod to reference another RetrievalMethod
mullan
parents:
5506
diff
changeset
|
100 |
atLeastOneFailed = true; |
3466
2200fa4b79ff
6858484: If an invalid HMAC XML Signature is validated, all subsequent valid HMAC signatures are invalid
mullan
parents:
3462
diff
changeset
|
101 |
} |
3462 | 102 |
} |
103 |
} |
|
104 |
||
105 |
private static void generate_hmac_sha1_40() throws Exception { |
|
106 |
System.out.println("Generating "); |
|
107 |
||
108 |
Document doc = dbf.newDocumentBuilder().newDocument(); |
|
109 |
XMLSignature sig = new XMLSignature |
|
110 |
(doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1, 40, |
|
111 |
Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS); |
|
112 |
try { |
|
113 |
sig.sign(getSecretKey("secret".getBytes("ASCII"))); |
|
114 |
System.out.println("FAILED"); |
|
115 |
atLeastOneFailed = true; |
|
116 |
} catch (XMLSignatureException xse) { |
|
117 |
System.out.println(xse.getMessage()); |
|
118 |
System.out.println("PASSED"); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
private static SecretKey getSecretKey(final byte[] secret) { |
|
123 |
return new SecretKey() { |
|
124 |
public String getFormat() { return "RAW"; } |
|
125 |
public byte[] getEncoded() { return secret; } |
|
126 |
public String getAlgorithm(){ return "SECRET"; } |
|
127 |
}; |
|
128 |
} |
|
129 |
} |