0N/A/*
2362N/A * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.crypto;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.security.*;
0N/Aimport java.util.jar.*;
0N/A
0N/A/**
0N/A * This class verifies JAR files (and any supporting JAR files), and
0N/A * determines whether they may be used in this implementation.
0N/A *
0N/A * The JCE in OpenJDK has an open cryptographic interface, meaning it
0N/A * does not restrict which providers can be used. Compliance with
0N/A * United States export controls and with local law governing the
0N/A * import/export of products incorporating the JCE in the OpenJDK is
0N/A * the responsibility of the licensee.
0N/A *
0N/A * @since 1.7
0N/A */
0N/Afinal class JarVerifier {
0N/A
0N/A // The URL for the JAR file we want to verify.
0N/A private URL jarURL;
0N/A private boolean savePerms;
0N/A private CryptoPermissions appPerms = null;
0N/A
0N/A /**
0N/A * Creates a JarVerifier object to verify the given URL.
0N/A *
0N/A * @param jarURL the JAR file to be verified.
0N/A * @param savePerms if true, save the permissions allowed by the
0N/A * exemption mechanism
0N/A */
0N/A JarVerifier(URL jarURL, boolean savePerms) {
0N/A this.jarURL = jarURL;
0N/A this.savePerms = savePerms;
0N/A }
0N/A
0N/A /**
0N/A * Verify the JAR file is signed by an entity which has a certificate
0N/A * issued by a trusted CA.
0N/A *
0N/A * In OpenJDK, we just need to examine the "cryptoperms" file to see
0N/A * if any permissions were bundled together with this jar file.
0N/A */
0N/A void verify() throws JarException, IOException {
0N/A
0N/A // Short-circuit. If we weren't asked to save any, we're done.
0N/A if (!savePerms) {
0N/A return;
0N/A }
0N/A
0N/A // If the protocol of jarURL isn't "jar", we should
0N/A // construct a JAR URL so we can open a JarURLConnection
0N/A // for verifying this provider.
0N/A final URL url = jarURL.getProtocol().equalsIgnoreCase("jar")?
0N/A jarURL : new URL("jar:" + jarURL.toString() + "!/");
0N/A
0N/A JarFile jf = null;
0N/A try {
0N/A
0N/A // Get a link to the Jarfile to search.
0N/A try {
0N/A jf = (JarFile)
0N/A AccessController.doPrivileged(
0N/A new PrivilegedExceptionAction() {
0N/A public Object run() throws Exception {
0N/A JarURLConnection conn =
0N/A (JarURLConnection) url.openConnection();
0N/A // You could do some caching here as
0N/A // an optimization.
0N/A conn.setUseCaches(false);
0N/A return conn.getJarFile();
0N/A }
0N/A });
0N/A } catch (java.security.PrivilegedActionException pae) {
0N/A SecurityException se = new SecurityException(
0N/A "Cannot load " + url.toString());
0N/A se.initCause(pae);
0N/A throw se;
0N/A }
0N/A
0N/A if (jf != null) {
0N/A JarEntry je = jf.getJarEntry("cryptoPerms");
0N/A if (je == null) {
0N/A throw new JarException(
0N/A "Can not find cryptoPerms");
0N/A }
0N/A try {
0N/A appPerms = new CryptoPermissions();
0N/A appPerms.load(jf.getInputStream(je));
0N/A } catch (Exception ex) {
0N/A JarException jex =
0N/A new JarException("Cannot load/parse" +
0N/A jarURL.toString());
0N/A jex.initCause(ex);
0N/A throw jex;
0N/A }
0N/A }
0N/A } finally {
0N/A // Only call close() when caching is not enabled.
0N/A // Otherwise, exceptions will be thrown for all
0N/A // subsequent accesses of this cached jar.
0N/A if (jf != null) {
0N/A jf.close();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Verify that the provided certs include the
0N/A * framework signing certificate.
0N/A *
0N/A * @param certs the list of certs to be checked.
0N/A * @throws Exception if the list of certs did not contain
0N/A * the framework signing certificate
0N/A */
0N/A static void verifyPolicySigned(java.security.cert.Certificate[] certs)
0N/A throws Exception {
0N/A }
0N/A
0N/A /**
0N/A * Returns the permissions which are bundled with the JAR file,
0N/A * aka the "cryptoperms" file.
0N/A *
0N/A * NOTE: if this JarVerifier instance is constructed with "savePerms"
0N/A * equal to false, then this method would always return null.
0N/A */
0N/A CryptoPermissions getPermissions() {
0N/A return appPerms;
0N/A }
0N/A}