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