jdk/src/java.base/share/classes/javax/crypto/ProviderVerifier.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 26861 47dde7f5cf36
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@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, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
     2
 * Copyright (c) 2007, 2014, 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
/**
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    34
 * This class verifies Provider/Policy resources found at a URL
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    35
 * (currently only JAR files and any supporting JAR files), and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * determines whether they may be used in this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The JCE in OpenJDK has an open cryptographic interface, meaning it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * does not restrict which providers can be used.  Compliance with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * United States export controls and with local law governing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * import/export of products incorporating the JCE in the OpenJDK is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the responsibility of the licensee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    46
final class ProviderVerifier {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // The URL for the JAR file we want to verify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private URL jarURL;
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    50
    private Provider provider;
2
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
    /**
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    55
     * Creates a ProviderVerifier object to verify the given URL.
2
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
     */
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    61
    ProviderVerifier(URL jarURL, boolean savePerms) {
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    62
        this(jarURL, null, savePerms);
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    63
    }
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    64
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    65
    /**
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    66
     * Creates a ProviderVerifier object to verify the given URL.
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    67
     *
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    68
     * @param jarURL the JAR file to be verified
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    69
     * @param provider the corresponding provider.
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    70
     * @param savePerms if true, save the permissions allowed by the
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    71
     *          exemption mechanism
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    72
     */
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    73
    ProviderVerifier(URL jarURL, Provider provider, boolean savePerms) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.jarURL = jarURL;
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    75
        this.provider = provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.savePerms = savePerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Verify the JAR file is signed by an entity which has a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * issued by a trusted CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * In OpenJDK, we just need to examine the "cryptoperms" file to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * if any permissions were bundled together with this jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
    86
    void verify() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Short-circuit.  If we weren't asked to save any, we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (!savePerms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // If the protocol of jarURL isn't "jar", we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // construct a JAR URL so we can open a JarURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // for verifying this provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        final URL url = jarURL.getProtocol().equalsIgnoreCase("jar")?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        jarURL : new URL("jar:" + jarURL.toString() + "!/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        JarFile jf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // Get a link to the Jarfile to search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   104
                jf = AccessController.doPrivileged(
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   105
                         new PrivilegedExceptionAction<JarFile>() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   106
                             public JarFile run() throws Exception {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   107
                                 JarURLConnection conn =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   108
                                     (JarURLConnection) url.openConnection();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   109
                                 // 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
   110
                                 // an optimization.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   111
                                 conn.setUseCaches(false);
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   112
                                 return conn.getJarFile();
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   113
                             }
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   114
                         });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } catch (java.security.PrivilegedActionException pae) {
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   116
                throw new SecurityException("Cannot load " + url.toString(),
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   117
                    pae.getCause());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                JarEntry je = jf.getJarEntry("cryptoPerms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                if (je == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    throw new JarException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        "Can not find cryptoPerms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    appPerms = new CryptoPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    appPerms.load(jf.getInputStream(je));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    JarException jex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        new JarException("Cannot load/parse" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                            jarURL.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    jex.initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    throw jex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // Only call close() when caching is not enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Otherwise, exceptions will be thrown for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // subsequent accesses of this cached jar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (jf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                jf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Verify that the provided certs include the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * framework signing certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param certs the list of certs to be checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws Exception if the list of certs did not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          the framework signing certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static void verifyPolicySigned(java.security.cert.Certificate[] certs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   160
     * Returns true if the given provider is JDK trusted crypto provider
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   161
     * if the implementation supports fast-path verification.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   162
     */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   163
    static boolean isTrustedCryptoProvider(Provider provider) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   164
        return false;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   165
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   166
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 26861
diff changeset
   167
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns the permissions which are bundled with the JAR file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * aka the "cryptoperms" file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
26861
47dde7f5cf36 8058845: Update JCE environment for build improvements
wetmore
parents: 25859
diff changeset
   171
     * NOTE: if this ProviderVerifier instance is constructed with "savePerms"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * equal to false, then this method would always return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    CryptoPermissions getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return appPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
}