jdk/src/share/classes/javax/crypto/JarVerifier.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3353 ddbd63234844
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.crypto.CryptoPolicyParser.ParsingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class verifies JAR files (and any supporting JAR files), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * determines whether they may be used in this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The JCE in OpenJDK has an open cryptographic interface, meaning it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * does not restrict which providers can be used.  Compliance with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * United States export controls and with local law governing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * import/export of products incorporating the JCE in the OpenJDK is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the responsibility of the licensee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
final class JarVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // The URL for the JAR file we want to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private URL jarURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private boolean savePerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private CryptoPermissions appPerms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a JarVerifier object to verify the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param jarURL the JAR file to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param savePerms if true, save the permissions allowed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *          exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    JarVerifier(URL jarURL, boolean savePerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.jarURL = jarURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.savePerms = savePerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Verify the JAR file is signed by an entity which has a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * issued by a trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * In OpenJDK, we just need to examine the "cryptoperms" file to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * if any permissions were bundled together with this jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void verify() throws JarException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // Short-circuit.  If we weren't asked to save any, we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (!savePerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // If the protocol of jarURL isn't "jar", we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // construct a JAR URL so we can open a JarURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // for verifying this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        final URL url = jarURL.getProtocol().equalsIgnoreCase("jar")?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        jarURL : new URL("jar:" + jarURL.toString() + "!/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JarFile jf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // Get a link to the Jarfile to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                jf = (JarFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        new PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                            public Object run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                JarURLConnection conn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                    (JarURLConnection) url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                // You could do some caching here as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                // an optimization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                conn.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                return conn.getJarFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                SecurityException se = new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    "Cannot load " + url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                se.initCause(pae);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                JarEntry je = jf.getJarEntry("cryptoPerms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (je == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    throw new JarException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        "Can not find cryptoPerms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    appPerms = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    appPerms.load(jf.getInputStream(je));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    JarException jex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        new JarException("Cannot load/parse" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                            jarURL.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    jex.initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    throw jex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // Only call close() when caching is not enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // Otherwise, exceptions will be thrown for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // subsequent accesses of this cached jar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Verify that the provided JarEntry was indeed signed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * framework signing certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param je the URL of the jar entry to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @throws Exception if the jar entry was not signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          the proper certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static void verifyFrameworkSigned(URL je) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Verify that the provided certs include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * framework signing certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param certs the list of certs to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws Exception if the list of certs did not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          the framework signing certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    static void verifyPolicySigned(java.security.cert.Certificate[] certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Returns the permissions which are bundled with the JAR file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * aka the "cryptoperms" file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * NOTE: if this JarVerifier instance is constructed with "savePerms"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * equal to false, then this method would always return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    CryptoPermissions getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return appPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}