jdk/test/javax/xml/crypto/dsig/ErrorHandlerPermissions.java
changeset 30651 31a121d1df9e
equal deleted inserted replaced
30650:956d1166c36d 30651:31a121d1df9e
       
     1 /*
       
     2  * Copyright (c) 2015, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 import java.io.ByteArrayInputStream;
       
    27 import java.io.File;
       
    28 import java.security.KeyFactory;
       
    29 import java.security.PublicKey;
       
    30 import java.security.spec.X509EncodedKeySpec;
       
    31 import java.util.Base64;
       
    32 import javax.xml.XMLConstants;
       
    33 import javax.xml.crypto.Data;
       
    34 import javax.xml.crypto.KeySelector;
       
    35 import javax.xml.crypto.OctetStreamData;
       
    36 import javax.xml.crypto.URIDereferencer;
       
    37 import javax.xml.crypto.URIReference;
       
    38 import javax.xml.crypto.URIReferenceException;
       
    39 import javax.xml.crypto.XMLCryptoContext;
       
    40 import javax.xml.crypto.dsig.XMLSignature;
       
    41 import javax.xml.crypto.dsig.XMLSignatureFactory;
       
    42 import javax.xml.crypto.dsig.dom.DOMValidateContext;
       
    43 import javax.xml.parsers.DocumentBuilderFactory;
       
    44 import org.w3c.dom.Document;
       
    45 import org.w3c.dom.Element;
       
    46 import org.w3c.dom.NodeList;
       
    47 
       
    48 /**
       
    49  * @test
       
    50  * @bug 8079140
       
    51  * @summary Check if IgnoreAllErrorHandler doesn't require additional permission
       
    52  * @run main/othervm/java.security.policy=ErrorHandlerPermissions.policy
       
    53  *                                                      ErrorHandlerPermissions
       
    54  */
       
    55 public class ErrorHandlerPermissions {
       
    56 
       
    57     private final static String FS = System.getProperty("file.separator");
       
    58     private final static String DIR = System.getProperty("test.src", ".");
       
    59     private final static String DATA_DIR = DIR + FS + "data";
       
    60     private final static String SIGNATURE = DATA_DIR + FS +
       
    61             "signature-external-rsa.xml";
       
    62 
       
    63     private static final String validationKey =
       
    64         "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnx4TdvPSA5vcsPi0OJZi9Ox0Z" +
       
    65         "2FRz2oeUCtuWoyEg0kUCeFd+jJZMstDJUiZNSOeuCO3FWSpdJgAwI4zlveHvuU/o" +
       
    66         "qHSa1eYTObOCvxfVYGGflWsSvGXyiANtRWVUrYODBeyL+2pWxDYh+Fi5EKizPfTG" +
       
    67         "wRjBVRSkRZKTnSjnQwIDAQAB";
       
    68 
       
    69     private static final URIDereferencer dereferencer =
       
    70             new DummyURIDereferencer();
       
    71 
       
    72     public static void main(String[] args) throws Exception {
       
    73         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       
    74         dbf.setNamespaceAware(true);
       
    75         dbf.setValidating(false);
       
    76         dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
       
    77         Document doc = dbf.newDocumentBuilder().parse(new File(SIGNATURE));
       
    78         NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS,
       
    79                 "Signature");
       
    80         if (nl.getLength() == 0) {
       
    81             throw new RuntimeException("Couldn't find 'Signature' element");
       
    82         }
       
    83         Element element = (Element) nl.item(0);
       
    84 
       
    85         byte[] keyBytes = Base64.getDecoder().decode(validationKey);
       
    86         X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
       
    87         KeyFactory kf = KeyFactory.getInstance("RSA");
       
    88         PublicKey key = kf.generatePublic(spec);
       
    89         KeySelector ks = KeySelector.singletonKeySelector(key);
       
    90 
       
    91         DOMValidateContext vc = new DOMValidateContext(ks, element);
       
    92 
       
    93         // disable secure validation mode
       
    94         vc.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.FALSE);
       
    95 
       
    96         // set a dummy dereferencer to be able to get content by references
       
    97         vc.setURIDereferencer(dereferencer);
       
    98 
       
    99         XMLSignatureFactory factory = XMLSignatureFactory.getInstance();
       
   100         XMLSignature signature = factory.unmarshalXMLSignature(vc);
       
   101 
       
   102         // run validation
       
   103         signature.validate(vc);
       
   104     }
       
   105 
       
   106     /**
       
   107      * This URIDereferencer returns a static XML document.
       
   108      */
       
   109     private static class DummyURIDereferencer implements URIDereferencer {
       
   110 
       
   111         @Override
       
   112         public Data dereference(final URIReference ref, XMLCryptoContext ctx)
       
   113                 throws URIReferenceException {
       
   114             // return static content
       
   115             return new OctetStreamData(new ByteArrayInputStream(
       
   116                     "<test>test</test>".getBytes()), ref.getURI(),
       
   117                     ref.getType());
       
   118         }
       
   119     }
       
   120 
       
   121 }