J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 6436919 6460930 |
| 27 | * @summary check that XML Signatures can be generated and validated with |
| 28 | * SecurityManager enabled and default policy |
| 29 | * @author Sean Mullan |
| 30 | */ |
| 31 | import java.io.*; |
| 32 | import java.net.*; |
| 33 | import java.security.KeyPair; |
| 34 | import java.security.KeyPairGenerator; |
| 35 | import java.security.Policy; |
| 36 | import java.security.URIParameter; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Collections; |
| 39 | import javax.xml.crypto.dsig.*; |
| 40 | import javax.xml.crypto.dsig.dom.DOMSignContext; |
| 41 | import javax.xml.crypto.dsig.dom.DOMValidateContext; |
| 42 | import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec; |
| 43 | import javax.xml.crypto.dsig.spec.TransformParameterSpec; |
| 44 | import javax.xml.parsers.DocumentBuilder; |
| 45 | import javax.xml.parsers.DocumentBuilderFactory; |
| 46 | import org.w3c.dom.Document; |
| 47 | import org.w3c.dom.Element; |
| 48 | |
| 49 | public class XMLDSigWithSecMgr implements Runnable { |
| 50 | |
| 51 | private XMLSignatureFactory fac; |
| 52 | private DigestMethod sha1; |
| 53 | private CanonicalizationMethod withoutComments; |
| 54 | private DocumentBuilder db; |
| 55 | |
| 56 | private ServerSocket ss; |
| 57 | |
| 58 | private void setup() throws Exception { |
| 59 | ss = new ServerSocket(0); |
| 60 | Thread thr = new Thread(this); |
| 61 | thr.start(); |
| 62 | |
| 63 | fac = XMLSignatureFactory.getInstance(); |
| 64 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 65 | dbf.setNamespaceAware(true); |
| 66 | db = dbf.newDocumentBuilder(); |
| 67 | sha1 = fac.newDigestMethod(DigestMethod.SHA1, null); |
| 68 | withoutComments = fac.newCanonicalizationMethod |
| 69 | (CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null); |
| 70 | } |
| 71 | |
| 72 | public void run() { |
| 73 | try { |
| 74 | |
| 75 | for (int i=0; i<2; i++) { |
| 76 | Socket s = ss.accept(); |
| 77 | s.setTcpNoDelay(true); |
| 78 | |
| 79 | PrintStream out = new PrintStream( |
| 80 | new BufferedOutputStream( |
| 81 | s.getOutputStream() )); |
| 82 | |
| 83 | out.print("HTTP/1.1 200 OK\r\n"); |
| 84 | out.print("Content-Length: 11\r\n"); |
| 85 | out.print("Content-Type: text/plain\r\n"); |
| 86 | out.print("\r\n"); |
| 87 | out.print("l;ajfdjafd\n"); |
| 88 | out.flush(); |
| 89 | |
| 90 | // don't close the connection immediately as otherwise |
| 91 | // the http headers may not have been received and the |
| 92 | // http client will re-connect. |
| 93 | Thread.currentThread().sleep(2000); |
| 94 | |
| 95 | s.close(); |
| 96 | } |
| 97 | |
| 98 | } catch (Exception e) { |
| 99 | e.printStackTrace(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | XMLDSigWithSecMgr() throws Exception { |
| 104 | setup(); |
| 105 | Document doc = db.newDocument(); |
| 106 | Element envelope = doc.createElementNS |
| 107 | ("http://example.org/envelope", "Envelope"); |
| 108 | envelope.setAttributeNS("http://www.w3.org/2000/xmlns/", |
| 109 | "xmlns", "http://example.org/envelope"); |
| 110 | doc.appendChild(envelope); |
| 111 | |
| 112 | KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); |
| 113 | KeyPair kp = kpg.genKeyPair(); |
| 114 | |
| 115 | // the policy only grants this test SocketPermission to accept, resolve |
| 116 | // and connect to localhost so that it can dereference 2nd reference |
| 117 | URI policyURI = |
| 118 | new File(System.getProperty("test.src", "."), "policy").toURI(); |
| 119 | Policy.setPolicy |
| 120 | (Policy.getInstance("JavaPolicy", new URIParameter(policyURI))); |
| 121 | System.setSecurityManager(new SecurityManager()); |
| 122 | |
| 123 | try { |
| 124 | // generate a signature with SecurityManager enabled |
| 125 | ArrayList refs = new ArrayList(); |
| 126 | refs.add(fac.newReference |
| 127 | ("", sha1, |
| 128 | Collections.singletonList |
| 129 | (fac.newTransform(Transform.ENVELOPED, |
| 130 | (TransformParameterSpec) null)), null, null)); |
| 131 | refs.add(fac.newReference("http://localhost:" + ss.getLocalPort() |
| 132 | + "/anything.txt", sha1)); |
| 133 | SignedInfo si = fac.newSignedInfo(withoutComments, |
| 134 | fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), refs); |
| 135 | XMLSignature sig = fac.newXMLSignature(si, null); |
| 136 | DOMSignContext dsc = new DOMSignContext(kp.getPrivate(), envelope); |
| 137 | sig.sign(dsc); |
| 138 | |
| 139 | // validate a signature with SecurityManager enabled |
| 140 | DOMValidateContext dvc = new DOMValidateContext |
| 141 | (kp.getPublic(), envelope.getFirstChild()); |
| 142 | sig = fac.unmarshalXMLSignature(dvc); |
| 143 | if (!sig.validate(dvc)) { |
| 144 | throw new Exception |
| 145 | ("XMLDSigWithSecMgr signature validation FAILED"); |
| 146 | } |
| 147 | } catch (SecurityException se) { |
| 148 | throw new Exception("XMLDSigWithSecMgr FAILED", se); |
| 149 | } |
| 150 | ss.close(); |
| 151 | } |
| 152 | |
| 153 | public static void main(String[] args) throws Exception { |
| 154 | new XMLDSigWithSecMgr(); |
| 155 | } |
| 156 | } |