jdk/src/java.base/share/classes/java/security/SecureClassLoader.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 41911 b3bb62588635
child 45434 4582657c7260
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, 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: 2448
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: 2448
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: 2448
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2448
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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    28
import java.util.Map;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    29
import java.util.Objects;
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
    30
import java.util.concurrent.ConcurrentHashMap;
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
    31
import java.util.function.Function;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.Debug;
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 extends ClassLoader with additional support for defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * classes with an associated code source and permissions which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * retrieved by the system policy by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class SecureClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * If initialization succeed this is set to true and security checks will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * succeed. Otherwise the object is not initialized and the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    49
    private final boolean initialized;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    51
    /*
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    52
     * Map that maps the CodeSource to a ProtectionDomain. The key is a
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    53
     * CodeSourceKey class that uses a String instead of a URL to avoid
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    54
     * potential expensive name service lookups. This does mean that URLs that
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    55
     * are equivalent after nameservice lookup will be placed in separate
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    56
     * ProtectionDomains; however during policy enforcement these URLs will be
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    57
     * canonicalized and resolved resulting in a consistent set of granted
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    58
     * permissions.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    59
     */
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    60
    private final Map<CodeSourceKey, ProtectionDomain> pdcache
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
    61
            = new ConcurrentHashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    63
    static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    64
        ClassLoader.registerAsParallelCapable();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    65
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    66
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a new SecureClassLoader using the specified parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * class loader for delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>If there is a security manager, this method first
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
    72
     * calls the security manager's {@code checkCreateClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * method  to ensure creation of a class loader is allowed.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
    74
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param parent the parent ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @exception  SecurityException  if a security manager exists and its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
    77
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected SecureClassLoader(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Creates a new SecureClassLoader using the default parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * loader for delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>If there is a security manager, this method first
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
    96
     * calls the security manager's {@code checkCreateClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * method  to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @exception  SecurityException  if a security manager exists and its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   100
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected SecureClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   115
     * Creates a new {@code SecureClassLoader} of the specified name and
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   116
     * using the specified parent class loader for delegation.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   117
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   118
     * @param name class loader name; or {@code null} if not named
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   119
     * @param parent the parent class loader
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   120
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   121
     * @throws IllegalArgumentException if the given name is empty.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   122
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   123
     * @throws SecurityException  if a security manager exists and its
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   124
     *         {@link SecurityManager#checkCreateClassLoader()} method
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   125
     *         doesn't allow creation of a class loader.
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   126
     *
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   127
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 41911
diff changeset
   128
     * @spec JPMS
41911
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   129
     */
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   130
    protected SecureClassLoader(String name, ClassLoader parent) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   131
        super(name, parent);
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   132
        SecurityManager security = System.getSecurityManager();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   133
        if (security != null) {
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   134
            security.checkCreateClassLoader();
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   135
        }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   136
        initialized = true;
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   137
    }
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   138
b3bb62588635 6479237: (cl) Add support for classloader names
mchung
parents: 37882
diff changeset
   139
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Converts an array of bytes into an instance of class Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * with an optional CodeSource. Before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * If a non-null CodeSource is supplied a ProtectionDomain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * constructed and associated with the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   146
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   147
     * @param      name the expected name of the class, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                  if not known, using '.' and not '/' as the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *                  and without a trailing ".class" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param      b    the bytes that make up the class data. The bytes in
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   151
     *             positions {@code off} through {@code off+len-1}
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   152
     *             should have the format of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   153
     *             <cite>The Java&trade; Virtual Machine Specification</cite>.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   154
     * @param      off  the start offset in {@code b} of the class data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param      len  the length of the class data
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   156
     * @param      cs   the associated CodeSource, or {@code null} if none
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   157
     * @return the {@code Class} object created from the data,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         and optional CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @exception  ClassFormatError if the data did not contain a valid class
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   160
     * @exception  IndexOutOfBoundsException if either {@code off} or
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   161
     *             {@code len} is negative, or if
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   162
     *             {@code off+len} is greater than {@code b.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception  SecurityException if an attempt is made to add this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *             to a package that contains classes that were signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *             a different set of certificates than this class, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *             the class name begins with "java.".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected final Class<?> defineClass(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                         byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                         CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   173
        return defineClass(name, b, off, len, getProtectionDomain(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   177
     * Converts a {@link java.nio.ByteBuffer ByteBuffer}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   178
     * into an instance of class {@code Class}, with an optional CodeSource.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * If a non-null CodeSource is supplied a ProtectionDomain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * constructed and associated with the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   183
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   184
     * @param      name the expected name of the class, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *                  if not known, using '.' and not '/' as the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *                  and without a trailing ".class" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param      b    the bytes that make up the class data.  The bytes from positions
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   188
     *                  {@code b.position()} through {@code b.position() + b.limit() -1}
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   189
     *                  should have the format of a valid class file as defined by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7970
diff changeset
   190
     *                  <cite>The Java&trade; Virtual Machine Specification</cite>.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   191
     * @param      cs   the associated CodeSource, or {@code null} if none
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   192
     * @return the {@code Class} object created from the data,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *         and optional CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @exception  ClassFormatError if the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception  SecurityException if an attempt is made to add this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *             to a package that contains classes that were signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *             a different set of certificates than this class, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *             the class name begins with "java.".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                         CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   205
        return defineClass(name, b, getProtectionDomain(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Returns the permissions for the given CodeSource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * This method is invoked by the defineClass method which takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * a CodeSource as an argument when it is constructing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * ProtectionDomain for the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   214
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param codesource the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return the permissions granted to the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return new Permissions(); // ProtectionDomain defers the binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /*
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   227
     * holder class for the static field "debug" to delay its initialization
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   228
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   229
    private static class DebugHolder {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   230
        private static final Debug debug = Debug.getInstance("scl");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   231
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   232
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   233
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returned cached ProtectionDomain for the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private ProtectionDomain getProtectionDomain(CodeSource cs) {
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   237
        if (cs == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return null;
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   239
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   241
        // Use a CodeSourceKey object key. It should behave in the
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   242
        // same manner as the CodeSource when compared for equality except
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   243
        // that no nameservice lookup is done on the hostname (String comparison
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   244
        // only), and the fragment is not considered.
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   245
        CodeSourceKey key = new CodeSourceKey(cs);
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   246
        return pdcache.computeIfAbsent(key, new Function<>() {
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   247
            @Override
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   248
            public ProtectionDomain apply(CodeSourceKey key /* not used */) {
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   249
                PermissionCollection perms
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   250
                        = SecureClassLoader.this.getPermissions(cs);
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   251
                ProtectionDomain pd = new ProtectionDomain(
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   252
                        cs, perms, SecureClassLoader.this, null);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   253
                if (DebugHolder.debug != null) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   254
                    DebugHolder.debug.println(" getPermissions " + pd);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   255
                    DebugHolder.debug.println("");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                }
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   257
                return pd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   259
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Check to make sure the class loader has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private void check() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw new SecurityException("ClassLoader object not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   271
    private static class CodeSourceKey {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   272
        private final CodeSource cs;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   273
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   274
        CodeSourceKey(CodeSource cs) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   275
            this.cs = cs;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   276
        }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   277
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   278
        @Override
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   279
        public int hashCode() {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   280
            String locationNoFrag = cs.getLocationNoFragString();
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   281
            return locationNoFrag != null ? locationNoFrag.hashCode() : 0;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   282
        }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   283
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   284
        @Override
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   285
        public boolean equals(Object obj) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   286
            if (obj == this) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   287
                return true;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   288
            }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   289
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   290
            if (!(obj instanceof CodeSourceKey)) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   291
                return false;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   292
            }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   293
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   294
            CodeSourceKey csk = (CodeSourceKey) obj;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   295
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   296
            if (!Objects.equals(cs.getLocationNoFragString(),
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   297
                                csk.cs.getLocationNoFragString())) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   298
                return false;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   299
            }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   300
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   301
            return cs.matchCerts(csk.cs, true);
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   302
        }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   303
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}