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+ }
0 commit comments