jdk/src/java.base/share/classes/java/security/SecureClassLoader.java
author sherman
Tue, 10 May 2016 21:19:25 -0700
changeset 37882 e7f3cf12e739
parent 31804 013c14047253
child 41911 b3bb62588635
permissions -rw-r--r--
6328855: String: Matches hangs at short and easy Strings containing \r \n 6192895: java.util.regex.Matcher: Performance issue 6345469: java.util.regex.Matcher utilizes 100% of the CPU 6988218: RegEx matcher loops 6693451: RegEx matcher goes into infinite delay 7006761: Matcher.matches() has infinite loop 8140212: Slow performance of Matcher.find 8151481: j.u.regex.Pattern cleanup 6609854: Regex does not match correctly for negative nested character classes 4916384: CANON_EQ supports only combining character sequences with non-spacing marks 4867170: Pattern doesn't work with composite character in CANON_EQ mode 6995635: CANON_EQ pattern flag is buggy 6728861: ExceptionInInitializerError is caught when the pattern has precomposed character 6736245: A character in Composition Exclusion Table does not match itself 7080302: the normalization in java regex pattern may have flaw Reviewed-by: rriggs, okutsu, alanb
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.net.URL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    30
import java.util.Map;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    31
import java.util.Objects;
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
    32
import java.util.concurrent.ConcurrentHashMap;
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
    33
import java.util.function.Function;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class extends ClassLoader with additional support for defining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * classes with an associated code source and permissions which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * retrieved by the system policy by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class SecureClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * If initialization succeed this is set to true and security checks will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * succeed. Otherwise the object is not initialized and the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    51
    private final boolean initialized;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    53
    /*
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    54
     * 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
    55
     * 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
    56
     * 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
    57
     * 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
    58
     * 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
    59
     * 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
    60
     * permissions.
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
    61
     */
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
    62
    private final Map<CodeSourceKey, ProtectionDomain> pdcache
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
    63
            = new ConcurrentHashMap<>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    65
    static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    66
        ClassLoader.registerAsParallelCapable();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    67
    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a new SecureClassLoader using the specified parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * class loader for delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <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
    74
     * calls the security manager's {@code checkCreateClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * 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
    76
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param parent the parent ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @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
    79
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected SecureClassLoader(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates a new SecureClassLoader using the default parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * loader for delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <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
    98
     * calls the security manager's {@code checkCreateClassLoader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * method  to ensure creation of a class loader is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @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
   102
     *             {@code checkCreateClassLoader} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *             creation of a class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see SecurityManager#checkCreateClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected SecureClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // this is to make the stack depth consistent with 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Converts an array of bytes into an instance of class Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * with an optional CodeSource. Before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * If a non-null CodeSource is supplied a ProtectionDomain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * 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
   123
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   124
     * @param      name the expected name of the class, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *                  if not known, using '.' and not '/' as the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *                  and without a trailing ".class" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @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
   128
     *             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
   129
     *             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
   130
     *             <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
   131
     * @param      off  the start offset in {@code b} of the class data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @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
   133
     * @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
   134
     * @return the {@code Class} object created from the data,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         and optional CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @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
   137
     * @exception  IndexOutOfBoundsException if either {@code off} or
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   138
     *             {@code len} is negative, or if
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   139
     *             {@code off+len} is greater than {@code b.length}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception  SecurityException if an attempt is made to add this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *             to a package that contains classes that were signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *             a different set of certificates than this class, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *             the class name begins with "java.".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected final Class<?> defineClass(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                         byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                         CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   150
        return defineClass(name, b, off, len, getProtectionDomain(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   154
     * Converts a {@link java.nio.ByteBuffer ByteBuffer}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   155
     * into an instance of class {@code Class}, with an optional CodeSource.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * If a non-null CodeSource is supplied a ProtectionDomain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * 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
   160
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 9275
diff changeset
   161
     * @param      name the expected name of the class, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                  if not known, using '.' and not '/' as the separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                  and without a trailing ".class" suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @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
   165
     *                  {@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
   166
     *                  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
   167
     *                  <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
   168
     * @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
   169
     * @return the {@code Class} object created from the data,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         and optional CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception  ClassFormatError if the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception  SecurityException if an attempt is made to add this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *             to a package that contains classes that were signed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *             a different set of certificates than this class, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *             the class name begins with "java.".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                         CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 2
diff changeset
   182
        return defineClass(name, b, getProtectionDomain(cs));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the permissions for the given CodeSource object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * This method is invoked by the defineClass method which takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * a CodeSource as an argument when it is constructing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * ProtectionDomain for the class being defined.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   191
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param codesource the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return the permissions granted to the codesource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return new Permissions(); // ProtectionDomain defers the binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /*
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   204
     * 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
   205
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   206
    private static class DebugHolder {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   207
        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
   208
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   209
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   210
    /*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Returned cached ProtectionDomain for the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private ProtectionDomain getProtectionDomain(CodeSource cs) {
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   214
        if (cs == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return null;
31141
ef5ddf021137 6826789: SecureClassLoader should not use CodeSource URLs as HashMap keys
mullan
parents: 29492
diff changeset
   216
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   218
        // 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
   219
        // 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
   220
        // that no nameservice lookup is done on the hostname (String comparison
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   221
        // 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
   222
        CodeSourceKey key = new CodeSourceKey(cs);
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   223
        return pdcache.computeIfAbsent(key, new Function<>() {
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   224
            @Override
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   225
            public ProtectionDomain apply(CodeSourceKey key /* not used */) {
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   226
                PermissionCollection perms
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   227
                        = SecureClassLoader.this.getPermissions(cs);
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   228
                ProtectionDomain pd = new ProtectionDomain(
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   229
                        cs, perms, SecureClassLoader.this, null);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   230
                if (DebugHolder.debug != null) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   231
                    DebugHolder.debug.println(" getPermissions " + pd);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 31804
diff changeset
   232
                    DebugHolder.debug.println("");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   234
                return pd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
31183
eb0ffbecb4ba 8064890: SecureClassLoader should use a ConcurrentHashMap
weijun
parents: 31141
diff changeset
   236
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Check to make sure the class loader has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private void check() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new SecurityException("ClassLoader object not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
31804
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   248
    private static class CodeSourceKey {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   249
        private final CodeSource cs;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   250
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   251
        CodeSourceKey(CodeSource cs) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   252
            this.cs = cs;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   253
        }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   254
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   255
        @Override
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   256
        public int hashCode() {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   257
            String locationNoFrag = cs.getLocationNoFragString();
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   258
            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
   259
        }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   260
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   261
        @Override
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   262
        public boolean equals(Object obj) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   263
            if (obj == this) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   264
                return true;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   265
            }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   266
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   267
            if (!(obj instanceof CodeSourceKey)) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   268
                return false;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   269
            }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   270
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   271
            CodeSourceKey csk = (CodeSourceKey) obj;
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   272
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   273
            if (!Objects.equals(cs.getLocationNoFragString(),
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   274
                                csk.cs.getLocationNoFragString())) {
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   275
                return false;
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
            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
   279
        }
013c14047253 8131486: SecureClassLoader key for ProtectionDomain cache also needs to take into account certificates
mullan
parents: 31183
diff changeset
   280
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}