Skip to content

Commit b08aae1

Browse files
committed
8272908: Missing coverage for certain classes in com.sun.org.apache.xml.internal.security
Backport-of: a16f2d0a3c326dd8b3b2133a9c170d998b7aa631
1 parent 708017c commit b08aae1

File tree

3 files changed

+253
-7
lines changed

3 files changed

+253
-7
lines changed
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact 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.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8272908
27+
* @summary Verify signature KeyInfo
28+
* @library /test/lib
29+
* @modules java.xml.crypto/com.sun.org.apache.xml.internal.security
30+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.c14n
31+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.signature
32+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.utils
33+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.keys
34+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.keys.content.keyvalues
35+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.keys.content
36+
* java.xml.crypto/com.sun.org.apache.xml.internal.security.exceptions
37+
* @run main/othervm SignatureKeyInfo
38+
*/
39+
40+
import com.sun.org.apache.xml.internal.security.Init;
41+
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
42+
import com.sun.org.apache.xml.internal.security.keys.KeyInfo;
43+
import com.sun.org.apache.xml.internal.security.keys.content.PGPData;
44+
import com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod;
45+
import com.sun.org.apache.xml.internal.security.keys.content.SPKIData;
46+
import com.sun.org.apache.xml.internal.security.signature.XMLSignature;
47+
import com.sun.org.apache.xml.internal.security.utils.Constants;
48+
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
49+
import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
50+
import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.RSAKeyValue;
51+
import com.sun.org.apache.xml.internal.security.keys.content.keyvalues.DSAKeyValue;
52+
53+
import jdk.test.lib.Asserts;
54+
import org.w3c.dom.Document;
55+
import org.w3c.dom.Element;
56+
import org.w3c.dom.NodeList;
57+
58+
59+
import javax.xml.crypto.dsig.CanonicalizationMethod;
60+
import javax.xml.crypto.dsig.SignatureMethod;
61+
import javax.xml.parsers.DocumentBuilderFactory;
62+
import java.io.File;
63+
import java.math.BigInteger;
64+
import java.security.*;
65+
66+
import static jdk.test.lib.Asserts.assertEquals;
67+
68+
public class SignatureKeyInfo {
69+
70+
private final static String DIR = System.getProperty("test.src", ".");
71+
private static DocumentBuilderFactory dbf = null;
72+
private static Document doc;
73+
74+
private static final String NAME = "testName";
75+
private static final String TEXT = "testText";
76+
private static final String NS = Constants.SignatureSpecNS;
77+
private static final String RSA = "RSA";
78+
private static final String DSA = "DSA";
79+
private static final String FILE_TO_SIGN = "signature-enveloping-hmac-sha1.xml";
80+
private static final String FILE_TO_VERIFY = "signature-enveloping-hmac-sha1-keyinfo.xml";
81+
private static final int FIRST_EL = 0;
82+
83+
public static void main(String[] args) throws Exception {
84+
85+
Init.init();
86+
dbf = DocumentBuilderFactory.newInstance();
87+
dbf.setNamespaceAware(true);
88+
dbf.setValidating(false);
89+
verifyXmlKeyInfo();
90+
sign(RSA);
91+
sign(DSA);
92+
}
93+
94+
private static void sign(String algorithm) throws Exception {
95+
File file = new File(DIR, FILE_TO_SIGN);
96+
97+
doc = dbf.newDocumentBuilder().parse(file);
98+
99+
KeyPair kp = getKeyPair(algorithm);
100+
101+
String signMethod = RSA.equals(algorithm) ? SignatureMethod.RSA_SHA256
102+
: SignatureMethod.DSA_SHA256;
103+
104+
XMLSignature signature = new XMLSignature(doc, null,
105+
signMethod, CanonicalizationMethod.INCLUSIVE);
106+
107+
signature.addKeyInfo(kp.getPublic());
108+
KeyInfo keyInfo = signature.getKeyInfo();
109+
addKeyInfoData(keyInfo, algorithm);
110+
signature.sign(kp.getPrivate());
111+
}
112+
113+
private static Element getSignElement() {
114+
NodeList nl =
115+
doc.getElementsByTagNameNS(NS, "Signature");
116+
if (nl.getLength() == 0) {
117+
throw new RuntimeException("Could not find signature Element");
118+
}
119+
120+
return (Element) nl.item(FIRST_EL);
121+
}
122+
123+
private static void addKeyInfoData(KeyInfo keyInfo, String algorithm) throws Exception {
124+
KeyPair keyPair = getKeyPair(algorithm);
125+
126+
if (algorithm.equals(RSA)) {
127+
RSAKeyValue rsaKeyValue = new RSAKeyValue(doc, keyPair.getPublic());
128+
keyInfo.add(rsaKeyValue);
129+
} else {
130+
DSAKeyValue dsaKeyValue = new DSAKeyValue(doc, keyPair.getPublic());
131+
keyInfo.add(dsaKeyValue);
132+
}
133+
134+
Element elpgp= doc.createElementNS(NS, Constants._TAG_PGPDATA);
135+
Element elrm= doc.createElementNS(NS, Constants._TAG_RETRIEVALMETHOD);
136+
Element elspki= doc.createElementNS(NS, Constants._TAG_SPKIDATA);
137+
keyInfo.add(new PGPData(elpgp, NS));
138+
keyInfo.add(new RetrievalMethod(elrm, NS));
139+
keyInfo.add(new SPKIData(elspki, NS));
140+
141+
keyInfo.setId(TEXT);
142+
keyInfo.addKeyName(TEXT);
143+
keyInfo.add(keyPair.getPublic());
144+
keyInfo.addKeyValue(keyPair.getPublic());
145+
keyInfo.addDEREncodedKeyValue(keyPair.getPublic());
146+
keyInfo.addKeyInfoReference(NS);
147+
keyInfo.addMgmtData(TEXT);
148+
149+
Element e = XMLUtils.createElementInSignatureSpace(doc, NAME);
150+
keyInfo.addKeyValue(e);
151+
keyInfo.addUnknownElement(e);
152+
keyInfo.addText(TEXT);
153+
keyInfo.addTextElement(TEXT, NAME);
154+
keyInfo.addBigIntegerElement(BigInteger.valueOf(12345), NAME);
155+
keyInfo.addBase64Text(TEXT.getBytes());
156+
keyInfo.addBase64Element(TEXT.getBytes(), NAME);
157+
158+
verifyKeyInfoData(keyInfo, algorithm);
159+
}
160+
161+
private static KeyPair getKeyPair(String algorithm) throws NoSuchAlgorithmException {
162+
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
163+
keyGen.initialize(2048);
164+
165+
return keyGen.genKeyPair();
166+
}
167+
168+
private static void verifyKeyInfoData(KeyInfo keyInfo, String algorithm)
169+
throws XMLSecurityException {
170+
Asserts.assertTrue(keyInfo.containsKeyName());
171+
verifyElementText(keyInfo.itemKeyName(FIRST_EL));
172+
Asserts.assertTrue(keyInfo.containsKeyValue());
173+
verifyElementNS(keyInfo.itemKeyValue(FIRST_EL).getBaseNamespace());
174+
175+
Asserts.assertTrue(keyInfo.containsKeyInfoReference());
176+
verifyElementNS(keyInfo.itemKeyInfoReference(FIRST_EL).getURI());
177+
Asserts.assertTrue(keyInfo.containsDEREncodedKeyValue());
178+
Asserts.assertTrue(keyInfo.containsMgmtData());
179+
verifyElementText(keyInfo.itemMgmtData(FIRST_EL));
180+
Asserts.assertEquals(TEXT, keyInfo.getId());
181+
182+
Asserts.assertTrue(keyInfo.containsPGPData());
183+
verifyElementNS(keyInfo.itemPGPData(FIRST_EL).getBaseNamespace());
184+
185+
Asserts.assertTrue(keyInfo.containsRetrievalMethod());
186+
verifyElementNS(keyInfo.itemRetrievalMethod(FIRST_EL).getBaseNamespace());
187+
Asserts.assertTrue(keyInfo.containsSPKIData());
188+
verifyElementNS(keyInfo.itemSPKIData(FIRST_EL).getBaseNamespace());
189+
190+
Asserts.assertTrue(keyInfo.containsUnknownElement());
191+
Asserts.assertEquals(NAME, keyInfo.itemUnknownElement(13).getLocalName());
192+
193+
Asserts.assertFalse(keyInfo.isEmpty());
194+
Asserts.assertEquals(algorithm, keyInfo.getPublicKey().getAlgorithm());
195+
}
196+
197+
private static void verifyXmlKeyInfo() throws Exception {
198+
File file = new File(DIR, FILE_TO_VERIFY);
199+
200+
doc = dbf.newDocumentBuilder().parse(file);
201+
Element sigElement = getSignElement();
202+
XMLSignature signature = new XMLSignature
203+
(sigElement, file.toURI().toString());
204+
205+
KeyInfo keyInfo = signature.getKeyInfo();
206+
assertEquals(TEXT, keyInfo.itemMgmtData(FIRST_EL).getMgmtData());
207+
}
208+
209+
private static void verifyElementText(ElementProxy elementProxy) {
210+
Asserts.assertEquals(TEXT, elementProxy.getTextFromTextChild());
211+
}
212+
213+
private static void verifyElementNS(String actualNs) {
214+
Asserts.assertEquals(NS, actualNs);
215+
}
216+
}

test/jdk/com/sun/org/apache/xml/internal/security/TruncateHMAC.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,6 +47,7 @@
4747
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
4848
import com.sun.org.apache.xml.internal.security.utils.Constants;
4949

50+
import static java.nio.charset.StandardCharsets.US_ASCII;
5051

5152
public class TruncateHMAC {
5253

@@ -64,7 +65,11 @@ public static void main(String[] args) throws Exception {
6465
validate("signature-enveloping-hmac-sha1-trunclen-8-attack.xml", false);
6566
// this one should pass
6667
validate("signature-enveloping-hmac-sha1.xml", true);
67-
generate_hmac_sha1_40();
68+
69+
// There are multiple validations regarding hmac min output length, therefore
70+
// checking different values will exercise multiple code blocks
71+
generate_hmac_sha1(40);
72+
generate_hmac_sha1(128);
6873

6974
if (atLeastOneFailed) {
7075
throw new Exception
@@ -86,7 +91,7 @@ private static void validate(String data, boolean pass) throws Exception {
8691
try {
8792
XMLSignature signature = new XMLSignature
8893
(sigElement, file.toURI().toString());
89-
SecretKey sk = signature.createSecretKey("secret".getBytes("ASCII"));
94+
SecretKey sk = signature.createSecretKey("secret".getBytes(US_ASCII));
9095
System.out.println
9196
("Validation status: " + signature.checkSignatureValue(sk));
9297
if (!pass) {
@@ -106,15 +111,15 @@ private static void validate(String data, boolean pass) throws Exception {
106111
}
107112
}
108113

109-
private static void generate_hmac_sha1_40() throws Exception {
110-
System.out.println("Generating ");
114+
private static void generate_hmac_sha1(int hmacOutputLength) throws Exception {
115+
System.out.println("Generating " + hmacOutputLength);
111116

112117
Document doc = dbf.newDocumentBuilder().newDocument();
113118
try {
114119
XMLSignature sig = new XMLSignature
115-
(doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1, 40,
120+
(doc, null, XMLSignature.ALGO_ID_MAC_HMAC_SHA1, hmacOutputLength,
116121
Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
117-
sig.sign(getSecretKey("secret".getBytes("ASCII")));
122+
sig.sign(getSecretKey("secret".getBytes(US_ASCII)));
118123
System.out.println("FAILED");
119124
atLeastOneFailed = true;
120125
} catch (XMLSignatureException xse) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
3+
<SignedInfo>
4+
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
5+
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#hmac-sha1" />
6+
<Reference URI="#object">
7+
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
8+
<DigestValue>7/XTsHaBSOnJ/jXD5v0zL6VKYsk=</DigestValue>
9+
</Reference>
10+
</SignedInfo>
11+
<SignatureValue>
12+
JElPttIT4Am7Q+MNoMyv+WDfAZw=
13+
</SignatureValue>
14+
<KeyInfo>
15+
<MgmtData>testText</MgmtData>
16+
<KeyValue>
17+
<ECKeyValue xmlns="http://www.w3.org/2009/xmldsig11#">
18+
<NamedCurve URI="urn:oid:1.2.840.10045.3.1.7"/>
19+
<PublicKey>BAds672US3sCYunM2k2bEQLbuRxdQlNTvq+5fitOpDMe0mBdZV4J3yZaG0taziYIuAT9GJGfds+q
20+
xtXOCNWe/60=</PublicKey>
21+
</ECKeyValue>
22+
</KeyValue>
23+
</KeyInfo>
24+
<Object Id="object">some text</Object>
25+
</Signature>

0 commit comments

Comments
 (0)