jdk/src/java.base/share/classes/javax/crypto/JarVerifier.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 10336 jdk/src/share/classes/javax/crypto/JarVerifier.java@0bb1999251f8
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3353
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class verifies JAR files (and any supporting JAR files), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * determines whether they may be used in this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The JCE in OpenJDK has an open cryptographic interface, meaning it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * does not restrict which providers can be used.  Compliance with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * United States export controls and with local law governing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * import/export of products incorporating the JCE in the OpenJDK is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the responsibility of the licensee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
final class JarVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // The URL for the JAR file we want to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private URL jarURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private boolean savePerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private CryptoPermissions appPerms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Creates a JarVerifier object to verify the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param jarURL the JAR file to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param savePerms if true, save the permissions allowed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *          exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    JarVerifier(URL jarURL, boolean savePerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.jarURL = jarURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.savePerms = savePerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Verify the JAR file is signed by an entity which has a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * issued by a trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * In OpenJDK, we just need to examine the "cryptoperms" file to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * if any permissions were bundled together with this jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    void verify() throws JarException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // Short-circuit.  If we weren't asked to save any, we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!savePerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // If the protocol of jarURL isn't "jar", we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // construct a JAR URL so we can open a JarURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // for verifying this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        final URL url = jarURL.getProtocol().equalsIgnoreCase("jar")?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        jarURL : new URL("jar:" + jarURL.toString() + "!/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        JarFile jf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // Get a link to the Jarfile to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    89
                jf = AccessController.doPrivileged(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    90
                         new PrivilegedExceptionAction<JarFile>() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    91
                             public JarFile run() throws Exception {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    92
                                 JarURLConnection conn =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    93
                                     (JarURLConnection) url.openConnection();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    94
                                 // You could do some caching here as
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    95
                                 // an optimization.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    96
                                 conn.setUseCaches(false);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    97
                                 return conn.getJarFile();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    98
                             }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
    99
                         });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } catch (java.security.PrivilegedActionException pae) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   101
                throw new SecurityException("Cannot load " + url.toString(), pae);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                JarEntry je = jf.getJarEntry("cryptoPerms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (je == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    throw new JarException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        "Can not find cryptoPerms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    appPerms = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    appPerms.load(jf.getInputStream(je));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    JarException jex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        new JarException("Cannot load/parse" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                            jarURL.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    jex.initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    throw jex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // Only call close() when caching is not enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // Otherwise, exceptions will be thrown for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // subsequent accesses of this cached jar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Verify that the provided certs include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * framework signing certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param certs the list of certs to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws Exception if the list of certs did not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *          the framework signing certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static void verifyPolicySigned(java.security.cert.Certificate[] certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns the permissions which are bundled with the JAR file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * aka the "cryptoperms" file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * NOTE: if this JarVerifier instance is constructed with "savePerms"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * equal to false, then this method would always return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    CryptoPermissions getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return appPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}