src/java.base/share/classes/sun/security/provider/PolicyFile.java
author coffeys
Wed, 06 Dec 2017 14:33:33 +0000
changeset 48080 18a4438eb690
parent 47478 438e0c9f2f17
child 48757 8cc67294ec56
permissions -rw-r--r--
8185855: Debug exception stacks should be clearer Reviewed-by: mullan, ascarpino
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
     2
 * Copyright (c) 1997, 2017, 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: 5194
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: 5194
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: 5194
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5194
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5194
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 sun.security.provider;
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.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URI;
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    33
import java.nio.file.Paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.NetPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.concurrent.atomic.AtomicReference;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31538
diff changeset
    44
import jdk.internal.misc.JavaSecurityProtectionDomainAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31538
diff changeset
    45
import static jdk.internal.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31538
diff changeset
    46
import jdk.internal.misc.SharedSecrets;
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
    47
import sun.security.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    51
 * This class represents a default Policy implementation for the
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    52
 * "JavaPolicy" type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Note:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * For backward compatibility with JAAS 1.0 it loads
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    56
 * both java.auth.policy and java.policy. However, it
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    57
 * is recommended that java.auth.policy not be used
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    58
 * and that java.policy contain all grant entries including
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    59
 * those that contain principal-based entries.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    61
 * <p> This object stores the policy for the entire Java runtime,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and is the amalgamation of multiple static policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * configurations that resides in files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The algorithm for locating the policy file(s) and reading their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * information into this <code>Policy</code> object is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <li>
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    69
 *   Read in and load the default policy file named
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    70
 *   &lt;JAVA_HOME&gt;/lib/security/default.policy. &lt;JAVA_HOME&gt; refers
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    71
 *   to the value of the java.home system property, and specifies the directory
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    72
 *   where the JRE is installed. This policy file grants permissions to the
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    73
 *   modules loaded by the platform class loader. If the default policy file
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    74
 *   cannot be loaded, a fatal InternalError is thrown as these permissions
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    75
 *   are needed in order for the runtime to operate correctly.
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    76
 * <li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   Loop through the <code>java.security.Security</code> properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   <i>policy.url.1</i>, <i>policy.url.2</i>, ...,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   <i>policy.url.X</i>" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   <i>auth.policy.url.1</i>, <i>auth.policy.url.2</i>, ...,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   <i>auth.policy.url.X</i>".  These properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   in the Java security properties file, which is located in the file named
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    83
 *   &lt;JAVA_HOME&gt;/conf/security/java.security.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   Each property value specifies a <code>URL</code> pointing to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   policy file to be loaded.  Read in and load each policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   <i>auth.policy.url</i> is supported only for backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    89
 *   If none of these could be loaded, use a builtin static policy
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    90
 *   equivalent to the conf/security/java.policy file.
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
    91
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   The <code>java.lang.System</code> property <i>java.security.policy</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   may also be set to a <code>URL</code> pointing to another policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   (which is the case when a user uses the -D switch at runtime).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   If this property is defined, and its use is allowed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   security property file (the Security property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   <i>policy.allowSystemProperty</i> is set to <i>true</i>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   also load that policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   The <code>java.lang.System</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   <i>java.security.auth.policy</i> may also be set to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   <code>URL</code> pointing to another policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   (which is the case when a user uses the -D switch at runtime).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   If this property is defined, and its use is allowed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   security property file (the Security property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   <i>policy.allowSystemProperty</i> is set to <i>true</i>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   also load that policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   <i>java.security.auth.policy</i> is supported only for backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *   compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   114
 *   If the <i>java.security.policy</i> or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *   <i>java.security.auth.policy</i> property is defined using
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   116
 *   "==" (rather than "="), then load the specified policy file and ignore
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   117
 *   all other configured policies. Note, that the default.policy file is
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   118
 *   also loaded, as specified in the first step of the algorithm above.
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   119
 *   If the specified policy file cannot be loaded, use a builtin static policy
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   120
 *   equivalent to the default conf/security/java.policy file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * Each policy file consists of one or more grant entries, each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * which consists of a number of permission entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *   grant signedBy "<b>alias</b>", codeBase "<b>URL</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *         principal <b>principalClass</b> "<b>principalName</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *         principal <b>principalClass</b> "<b>principalName</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *         ... {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *     permission <b>Type</b> "<b>name</b> "<b>action</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *         signedBy "<b>alias</b>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *     permission <b>Type</b> "<b>name</b> "<b>action</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *         signedBy "<b>alias</b>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *     ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * All non-bold items above must appear as is (although case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * doesn't matter and some are optional, as noted below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * principal entries are optional and need not be present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * Italicized items represent variable values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <p> A grant entry must begin with the word <code>grant</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * The <code>signedBy</code>,<code>codeBase</code> and <code>principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * name/value pairs are optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * If they are not present, then any signer (including unsigned code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * will match, and any codeBase will match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * Note that the <i>principalClass</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * may be set to the wildcard value, *, which allows it to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * any <code>Principal</code> class.  In addition, the <i>principalName</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * may also be set to the wildcard value, *, allowing it to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * any <code>Principal</code> name.  When setting the <i>principalName</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * to the *, do not surround the * with quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <p> A permission entry must begin with the word <code>permission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * The word <code><i>Type</i></code> in the template above is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * a specific permission type, such as <code>java.io.FilePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * or <code>java.lang.RuntimePermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <p> The "<i>action</i>" is required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * many permission types, such as <code>java.io.FilePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * (where it specifies what type of file access that is permitted).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * It is not required for categories such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <code>java.lang.RuntimePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * where it is not necessary - you either have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * permission specified by the <code>"<i>name</i>"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * value following the type name or you don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p> The <code>signedBy</code> name/value pair for a permission entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * is optional. If present, it indicates a signed permission. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * the permission class itself must be signed by the given alias in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * order for it to be granted. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * suppose you have the following grant entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *   grant principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *     permission Foo "foobar", signedBy "FooSoft";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <p> Then this permission of type <i>Foo</i> is granted if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <code>Foo.class</code> permission has been signed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * "FooSoft" alias, or if XXX <code>Foo.class</code> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * system class (i.e., is found on the CLASSPATH).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * <p> Items that appear in an entry must appear in the specified order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * (<code>permission</code>, <i>Type</i>, "<i>name</i>", and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * "<i>action</i>"). An entry is terminated with a semicolon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * <p> Case is unimportant for the identifiers (<code>permission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * <code>signedBy</code>, <code>codeBase</code>, etc.) but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * significant for the <i>Type</i>
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
   195
 * or for any string that is passed in as a value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * <p> An example of two entries in a policy configuration file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *   // if the code is comes from "foo.com" and is running as "Duke",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *   // grant it read/write to all files in /tmp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *   grant codeBase "foo.com", principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *              permission java.io.FilePermission "/tmp/*", "read,write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *   // grant any code running as "Duke" permission to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *   // the "java.vendor" Property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *   grant principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *         permission java.util.PropertyPermission "java.vendor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *  This Policy implementation supports special handling of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *  permission that contains the string, "<b>${{self}}</b>", as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *  its target name.  When such a permission is evaluated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *  (such as during a security check), <b>${{self}}</b> is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *  with one or more Principal class/name pairs.  The exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *  replacement performed depends upon the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *  grant clause to which the permission belongs.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
   221
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *  If the grant clause does not contain any principal information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *  the permission will be ignored (permissions containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *  <b>${{self}}</b> in their target names are only valid in the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *  of a principal-based grant clause).  For example, BarPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *  will always be ignored in the following grant clause:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
   229
 * <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *    grant codebase "www.foo.com", signedby "duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *      permission BarPermission "... ${{self}} ...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *    };
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
   233
 * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *  If the grant clause contains principal information, <b>${{self}}</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *  will be replaced with that same principal information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *  For example, <b>${{self}}</b> in BarPermission will be replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *  <b>javax.security.auth.x500.X500Principal "cn=Duke"</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *  in the following grant clause:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *    grant principal javax.security.auth.x500.X500Principal "cn=Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *      permission BarPermission "... ${{self}} ...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *  If there is a comma-separated list of principals in the grant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *  clause, then <b>${{self}}</b> will be replaced by the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *  comma-separated list or principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *  In the case where both the principal class and name are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *  wildcarded in the grant clause, <b>${{self}}</b> is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *  with all the principals associated with the <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *  in the current <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * <p> For PrivateCredentialPermissions, you can also use "<b>self</b>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * instead of "<b>${{self}}</b>". However the use of "<b>self</b>" is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * deprecated in favour of "<b>${{self}}</b>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * @see java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
public class PolicyFile extends java.security.Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static final Debug debug = Debug.getInstance("policy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private static final String SELF = "${{self}}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    private static final String X500PRINCIPAL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        "javax.security.auth.x500.X500Principal";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static final String POLICY = "java.security.policy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private static final String POLICY_URL = "policy.url.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final String AUTH_POLICY = "java.security.auth.policy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private static final String AUTH_POLICY_URL = "auth.policy.url.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static final int DEFAULT_CACHE_SIZE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // contains the policy grant entries, PD cache, and alias mapping
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 7179
diff changeset
   278
    private AtomicReference<PolicyInfo> policyInfo = new AtomicReference<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private boolean expandProperties = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private boolean allowSystemProperties = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private boolean notUtf8 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    // for use with the reflection API
22041
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 16046
diff changeset
   286
    private static final Class<?>[] PARAMS0 = { };
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 16046
diff changeset
   287
    private static final Class<?>[] PARAMS1 = { String.class };
3638966a5c02 8030082: Fix raw types lint warnings, etc. in various sun.security libraries
darcy
parents: 16046
diff changeset
   288
    private static final Class<?>[] PARAMS2 = { String.class, String.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   291
     * When a policy file has a syntax error, the exception code may generate
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   292
     * another permission check and this can cause the policy file to be parsed
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   293
     * repeatedly, leading to a StackOverflowError or ClassCircularityError.
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   294
     * To avoid this, this set is populated with policy files that have been
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   295
     * previously parsed and have syntax errors, so that they can be
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   296
     * subsequently ignored.
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   297
     */
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   298
    private static AtomicReference<Set<URL>> badPolicyURLs =
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   299
        new AtomicReference<>(new HashSet<>());
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   300
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   301
    // The default.policy file
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   302
    private static final URL DEFAULT_POLICY_URL =
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   303
        AccessController.doPrivileged(new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   304
            @Override
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   305
            public URL run() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   306
                String sep = File.separator;
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   307
                try {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   308
                    return Paths.get(System.getProperty("java.home"),
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   309
                                     "lib", "security",
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   310
                                     "default.policy").toUri().toURL();
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   311
                } catch (MalformedURLException mue) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   312
                    // should not happen
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   313
                    throw new Error("Malformed default.policy URL: " + mue);
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   314
                }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   315
            }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   316
        });
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   317
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   318
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Initializes the Policy object and reads the default policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * configuration file(s) into the Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public PolicyFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        init((URL)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Initializes the Policy object and reads the default policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * from the specified URL only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public PolicyFile(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        this.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        init(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Initializes the Policy object and reads the default policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * configuration file(s) into the Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   339
     * See the class description for details on the algorithm used to
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   340
     * initialize the Policy object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private void init(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Properties are set once for each init(); ignore changes between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // between diff invocations of initPolicyFile(policy, url, info).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        String numCacheStr =
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   346
          AccessController.doPrivileged(new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   347
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                expandProperties = "true".equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    (Security.getProperty("policy.expandProperties"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                allowSystemProperties = "true".equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    (Security.getProperty("policy.allowSystemProperty"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                notUtf8 = "false".equalsIgnoreCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    (System.getProperty("sun.security.policy.utf8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return System.getProperty("sun.security.policy.numcaches");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        int numCaches;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (numCacheStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                numCaches = Integer.parseInt(numCacheStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                numCaches = DEFAULT_CACHE_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            numCaches = DEFAULT_CACHE_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // System.out.println("number caches=" + numCaches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        PolicyInfo newInfo = new PolicyInfo(numCaches);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        initPolicyFile(newInfo, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        policyInfo.set(newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private void initPolicyFile(final PolicyInfo newInfo, final URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   376
        // always load default.policy
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   377
        if (debug != null) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   378
            debug.println("reading " + DEFAULT_POLICY_URL);
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   379
        }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   380
        AccessController.doPrivileged(new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   381
            @Override
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   382
            public Void run() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   383
                init(DEFAULT_POLICY_URL, newInfo, true);
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   384
                return null;
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   385
            }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   386
        });
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   387
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
             * If the caller specified a URL via Policy.getInstance,
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   392
             * we only read from default.policy and that URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (debug != null) {
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   396
                debug.println("reading " + url);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   398
            AccessController.doPrivileged(new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   399
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                public Void run() {
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   401
                    if (init(url, newInfo, false) == false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                        // use static policy if all else fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        initStaticPolicy(newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
             * Caller did not specify URL via Policy.getInstance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
             * Read from URLs listed in the java.security properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
             *
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   415
             * We call initPolicyFile with POLICY, POLICY_URL and then
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   416
             * call it with AUTH_POLICY and AUTH_POLICY_URL.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
             * So first we will process the JAVA standard policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
             * and then process the JAVA AUTH Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
             * This is for backward compatibility as well as to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
             * cases where the user has a single unified policyfile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
             * with both java policy entries and auth entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            boolean loaded_one = initPolicyFile(POLICY, POLICY_URL, newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // To maintain strict backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            // we load the static policy only if POLICY load failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (!loaded_one) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                // use static policy if all else fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                initStaticPolicy(newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            initPolicyFile(AUTH_POLICY, AUTH_POLICY_URL, newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private boolean initPolicyFile(final String propname, final String urlname,
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   437
                                   final PolicyInfo newInfo) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   438
        boolean loadedPolicy =
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   439
            AccessController.doPrivileged(new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   440
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                boolean loaded_policy = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (allowSystemProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    String extra_policy = System.getProperty(propname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    if (extra_policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        boolean overrideAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        if (extra_policy.startsWith("=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            overrideAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            extra_policy = extra_policy.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                            extra_policy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                PropertyExpander.expand(extra_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                            URL policyURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            File policyFile = new File(extra_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            if (policyFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                policyURL = ParseUtil.fileToEncodedURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                    (new File(policyFile.getCanonicalPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                policyURL = new URL(extra_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            }
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   464
                            if (debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                debug.println("reading "+policyURL);
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   466
                            }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   467
                            if (init(policyURL, newInfo, false)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                loaded_policy = true;
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   469
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                            // ignore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                debug.println("caught exception: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        if (overrideAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                                debug.println("overriding other policies!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                            return Boolean.valueOf(loaded_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                int n = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                String policy_uri;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                while ((policy_uri = Security.getProperty(urlname+n)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        URL policy_url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        String expanded_uri = PropertyExpander.expand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                (policy_uri).replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        if (policy_uri.startsWith("file:${java.home}/") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            policy_uri.startsWith("file:${user.home}/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                            // this special case accommodates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                            // the situation java.home/user.home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                            // expand to a single slash, resulting in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            // a file://foo URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                            policy_url = new File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                (expanded_uri.substring(5)).toURI().toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                            policy_url = new URI(expanded_uri).toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   507
                        if (debug != null) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   508
                            debug.println("reading " + policy_url);
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   509
                        }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   510
                        if (init(policy_url, newInfo, false)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                            loaded_policy = true;
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   512
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        if (debug != null) {
48080
18a4438eb690 8185855: Debug exception stacks should be clearer
coffeys
parents: 47478
diff changeset
   515
                            debug.println(
18a4438eb690 8185855: Debug exception stacks should be clearer
coffeys
parents: 47478
diff changeset
   516
                                "Debug info only. Error reading policy " +e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        // ignore that policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return Boolean.valueOf(loaded_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   527
        return loadedPolicy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Reads a policy configuration into the Policy object using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Reader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   534
    private boolean init(URL policy, PolicyInfo newInfo, boolean defPolicy) {
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   535
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   536
        // skip parsing policy file if it has been previously parsed and
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   537
        // has syntax errors
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   538
        if (badPolicyURLs.get().contains(policy)) {
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   539
            if (debug != null) {
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   540
                debug.println("skipping bad policy file: " + policy);
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   541
            }
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   542
            return false;
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   543
        }
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   544
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   545
        try (InputStreamReader isr =
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   546
                 getInputStreamReader(PolicyUtil.getInputStream(policy))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   548
            PolicyParser pp = new PolicyParser(expandProperties);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            pp.read(isr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            KeyStore keyStore = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                keyStore = PolicyUtil.getKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                (policy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                pp.getKeyStoreUrl(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                pp.getKeyStoreType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                pp.getKeyStoreProvider(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                pp.getStorePassURL(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                                debug);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                // ignore, treat it like we have no keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                if (debug != null) {
48080
18a4438eb690 8185855: Debug exception stacks should be clearer
coffeys
parents: 47478
diff changeset
   563
                    debug.println("Debug info only. Ignoring exception.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            Enumeration<PolicyParser.GrantEntry> enum_ = pp.grantElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                PolicyParser.GrantEntry ge = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                addGrantEntry(ge, keyStore, newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   573
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } catch (PolicyParser.ParsingException pe) {
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   575
            if (defPolicy) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   576
                throw new InternalError("Failed to load default.policy", pe);
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   577
            }
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   578
            // record bad policy file to avoid later reparsing it
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   579
            badPolicyURLs.updateAndGet(k -> {
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   580
                k.add(policy);
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   581
                return k;
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   582
            });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            Object[] source = {policy, pe.getLocalizedMessage()};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   584
            System.err.println(LocalizedMessage.getMessage
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   585
                (POLICY + ".error.parsing.policy.message", source));
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   586
            if (debug != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                pe.printStackTrace();
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 36511
diff changeset
   588
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        } catch (Exception e) {
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   590
            if (defPolicy) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   591
                throw new InternalError("Failed to load default.policy", e);
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   592
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                debug.println("error parsing "+policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                debug.println(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   600
        return false;
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   601
    }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   602
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   603
    private InputStreamReader getInputStreamReader(InputStream is)
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   604
                              throws IOException {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   605
        /*
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   606
         * Read in policy using UTF-8 by default.
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   607
         *
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   608
         * Check non-standard system property to see if the default encoding
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   609
         * should be used instead.
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   610
         */
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   611
        return (notUtf8)
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   612
            ? new InputStreamReader(is)
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   613
            : new InputStreamReader(is, "UTF-8");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private void initStaticPolicy(final PolicyInfo newInfo) {
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   617
        if (debug != null) {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   618
            debug.println("Initializing with static permissions");
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   619
        }
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   620
        AccessController.doPrivileged(new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
   621
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                PolicyEntry pe = new PolicyEntry(new CodeSource(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    (Certificate[]) null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                pe.add(SecurityConstants.LOCAL_LISTEN_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                pe.add(new PropertyPermission("java.version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                pe.add(new PropertyPermission("java.vendor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                pe.add(new PropertyPermission("java.vendor.url",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                pe.add(new PropertyPermission("java.class.version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                pe.add(new PropertyPermission("os.name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                pe.add(new PropertyPermission("os.version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                pe.add(new PropertyPermission("os.arch",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                pe.add(new PropertyPermission("file.separator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                pe.add(new PropertyPermission("path.separator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                pe.add(new PropertyPermission("line.separator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                pe.add(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                ("java.specification.version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                pe.add(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                                ("java.specification.vendor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                pe.add(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                ("java.specification.name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                pe.add(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                ("java.vm.specification.version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                pe.add(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                ("java.vm.specification.vendor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                pe.add(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                                ("java.vm.specification.name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                pe.add(new PropertyPermission("java.vm.version",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                pe.add(new PropertyPermission("java.vm.vendor",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                pe.add(new PropertyPermission("java.vm.name",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                // No need to sync because noone has access to newInfo yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                newInfo.policyEntries.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Given a GrantEntry, create a codeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @return null if signedBy alias is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    private CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        PolicyInfo newInfo) throws java.net.MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Certificate[] certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (ge.signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            certs = getCertificates(keyStore, ge.signedBy, newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                // we don't have a key for this alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                // just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    debug.println("  -- No certs for alias '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                       ge.signedBy + "' - ignoring entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        URL location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (ge.codeBase != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            location = new URL(ge.codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            location = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return (canonicalizeCodebase(new CodeSource(location, certs),false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Add one policy entry to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    private void addGrantEntry(PolicyParser.GrantEntry ge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                               KeyStore keyStore, PolicyInfo newInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            debug.println("Adding policy entry: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            debug.println("  signedBy " + ge.signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            debug.println("  codeBase " + ge.codeBase);
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   721
            if (ge.principals != null) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   722
                for (PolicyParser.PrincipalEntry pppe : ge.principals) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   723
                    debug.println("  " + pppe.toString());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            CodeSource codesource = getCodeSource(ge, keyStore, newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            // skip if signedBy alias was unknown...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (codesource == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            // perform keystore alias principal replacement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            // for example, if alias resolves to X509 certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            // replace principal with:  <X500Principal class>  <SubjectDN>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            // -- skip if alias is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (replacePrincipals(ge.principals, keyStore) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            PolicyEntry entry = new PolicyEntry(codesource, ge.principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            Enumeration<PolicyParser.PermissionEntry> enum_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                                ge.permissionElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                PolicyParser.PermissionEntry pe = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    // perform ${{ ... }} expansions within permission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    expandPermissionName(pe, keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    // XXX special case PrivateCredentialPermission-SELF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    Permission perm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    if (pe.permission.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        ("javax.security.auth.PrivateCredentialPermission") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        pe.name.endsWith(" self")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                        pe.name = pe.name.substring(0, pe.name.indexOf("self"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                + SELF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    // check for self
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    if (pe.name != null && pe.name.indexOf(SELF) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                        // Create a "SelfPermission" , it could be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                        // an unresolved permission which will be resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        // when implies is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        // Add it to entry
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30790
diff changeset
   763
                        Certificate[] certs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        if (pe.signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                            certs = getCertificates(keyStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                                    pe.signedBy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                                    newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                            certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        perm = new SelfPermission(pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                                  pe.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                                  pe.action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                                  certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        perm = getInstance(pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                           pe.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                           pe.action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    entry.add(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                        debug.println("  "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                } catch (ClassNotFoundException cnfe) {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30790
diff changeset
   785
                    Certificate[] certs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    if (pe.signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        certs = getCertificates(keyStore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                                                pe.signedBy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                                                newInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   794
                    // only add if we had no signer or we had
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    // a signer and found the keys for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    if (certs != null || pe.signedBy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        Permission perm = new UnresolvedPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                                                  pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                                  pe.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                                                  pe.action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                                                  certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                        entry.add(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                            debug.println("  "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                } catch (java.lang.reflect.InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    Object[] source = {pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                       ite.getTargetException().toString()};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   810
                    System.err.println(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   811
                        LocalizedMessage.getMessage(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   812
                            POLICY + ".error.adding.Permission.perm.message",
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   813
                            source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    Object[] source = {pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                                       e.toString()};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   817
                    System.err.println(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   818
                        LocalizedMessage.getMessage(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   819
                            POLICY + ".error.adding.Permission.perm.message",
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   820
                            source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            // No need to sync because noone has access to newInfo yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            newInfo.policyEntries.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            Object[] source = {e.toString()};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   828
            System.err.println(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   829
                LocalizedMessage.getMessage(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   830
                    POLICY + ".error.adding.Entry.message",
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
   831
                    source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Returns a new Permission object of the given Type. The Permission is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * created by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * Class object using the <code>Class.forName</code> method, and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * the reflection API to invoke the (String name, String actions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * constructor on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @param type the type of Permission being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @param name the name of the Permission being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @param actions the actions of the Permission being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @exception  ClassNotFoundException  if the particular Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *             class could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @exception  IllegalAccessException  if the class or initializer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *               not accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @exception  InstantiationException  if getInstance tries to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *               instantiate an abstract class or an interface, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *               instantiation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @exception  NoSuchMethodException if the (String, String) constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *               is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @exception  InvocationTargetException if the underlying Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *               constructor throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    private static final Permission getInstance(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                    String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                                    String actions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
               InstantiationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
               IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
               NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
               InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   876
        Class<?> pc = Class.forName(type, false, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   877
        Permission answer = getKnownPermission(pc, name, actions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if (answer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        }
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   881
        if (!Permission.class.isAssignableFrom(pc)) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   882
            // not the right subtype
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   883
            throw new ClassCastException(type + " is not a Permission");
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   884
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (name == null && actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                Constructor<?> c = pc.getConstructor(PARAMS0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                return (Permission) c.newInstance(new Object[] {});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            } catch (NoSuchMethodException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    Constructor<?> c = pc.getConstructor(PARAMS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                              new Object[] { name});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                } catch (NoSuchMethodException ne1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    Constructor<?> c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            if (name != null && actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    Constructor<?> c = pc.getConstructor(PARAMS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    return (Permission) c.newInstance(new Object[] { name});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                } catch (NoSuchMethodException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    Constructor<?> c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                    return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                          new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                Constructor<?> c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                return (Permission) c.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                      new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   920
     * Creates one of the well-known permissions in the java.base module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   921
     * directly instead of via reflection. Keep list short to not penalize
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   922
     * permissions from other modules.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   924
    private static Permission getKnownPermission(Class<?> claz, String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   925
                                                 String actions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (claz.equals(FilePermission.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            return new FilePermission(name, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        } else if (claz.equals(SocketPermission.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            return new SocketPermission(name, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        } else if (claz.equals(RuntimePermission.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            return new RuntimePermission(name, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        } else if (claz.equals(PropertyPermission.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            return new PropertyPermission(name, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        } else if (claz.equals(NetPermission.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            return new NetPermission(name, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        } else if (claz.equals(AllPermission.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            return SecurityConstants.ALL_PERMISSION;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   938
        } else if (claz.equals(SecurityPermission.class)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   939
            return new SecurityPermission(name, actions);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   940
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   941
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   942
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   943
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   944
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   945
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   946
     * Creates one of the well-known principals in the java.base module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   947
     * directly instead of via reflection. Keep list short to not penalize
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   948
     * principals from other modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   949
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   950
    private static Principal getKnownPrincipal(Class<?> claz, String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   951
        if (claz.equals(X500Principal.class)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
   952
            return new X500Principal(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * Fetch all certs associated with this alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    private Certificate[] getCertificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                (KeyStore keyStore, String aliases, PolicyInfo newInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        List<Certificate> vcerts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        StringTokenizer st = new StringTokenizer(aliases, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            String alias = st.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            // See if this alias's cert has already been cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            synchronized (newInfo.aliasMapping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                cert = (Certificate)newInfo.aliasMapping.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                if (cert == null && keyStore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                        cert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                        // never happens, because keystore has already been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                        // when we call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                        newInfo.aliasMapping.put(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                        newInfo.aliasMapping.put(cert, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                if (vcerts == null)
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
   994
                    vcerts = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                vcerts.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        // make sure n == vcerts.size, since we are doing a logical *and*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if (vcerts != null && n == vcerts.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            Certificate[] certs = new Certificate[vcerts.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            vcerts.toArray(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Refreshes the policy object by re-reading all the policy files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  1012
    @Override public void refresh() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        init(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27565
diff changeset
  1017
     * Evaluates the global policy for the permissions granted to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * the ProtectionDomain and tests whether the permission is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * granted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
  1021
     * @param pd the ProtectionDomain to test
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
  1022
     * @param p the Permission object to be tested for implication.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @return true if "permission" is a proper subset of a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * granted to this ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  1029
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    public boolean implies(ProtectionDomain pd, Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        PolicyInfo pi = policyInfo.get();
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  1032
        ProtectionDomainCache pdMap = pi.getPdMapping();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        PermissionCollection pc = pdMap.get(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (pc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            return pc.implies(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        pc = getPermissions(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        if (pc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // cache mapping of protection domain to its PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        pdMap.put(pd, pc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        return pc.implies(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Examines this <code>Policy</code> and returns the permissions granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * to the specified <code>ProtectionDomain</code>.  This includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * the permissions currently associated with the domain as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * as the policy permissions granted to the domain's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * CodeSource, ClassLoader, and Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * <p> Note that this <code>Policy</code> implementation has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * special handling for PrivateCredentialPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * When this method encounters a <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * which specifies "self" as the <code>Principal</code> class and name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * it does not add that <code>Permission</code> to the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <code>PermissionCollection</code>.  Instead, it builds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * a new <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * for each <code>Principal</code> associated with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * <code>Subject</code>.  Each new <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * contains the same Credential class as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * originally granted permission, as well as the Class and name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * for the respective <code>Principal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @param domain the Permissions granted to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *          <code>ProtectionDomain</code> are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * @return the Permissions granted to the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *          <code>ProtectionDomain</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  1076
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    public PermissionCollection getPermissions(ProtectionDomain domain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        if (domain == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
           return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        // first get policy perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        getPermissions(perms, domain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // add static perms
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        //      - adding static perms after policy perms is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        //        to avoid a regression for 4301064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        PermissionCollection pc = domain.getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (pc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            synchronized (pc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                Enumeration<Permission> e = pc.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                while (e.hasMoreElements()) {
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1094
                    perms.add(FilePermCompat.newPermPlusAltPath(e.nextElement()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * Examines this Policy and creates a PermissionCollection object with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * the set of permissions for the specified CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
  1106
     * @param codesource the CodeSource associated with the caller.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * This encapsulates the original location of the code (where the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * came from) and the public key(s) of its signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @return the set of permissions according to the policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  1112
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    public PermissionCollection getPermissions(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        return getPermissions(new Permissions(), codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Examines the global policy and returns the provided Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * object with additional permissions granted to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1122
     * @param perms the Permissions to populate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @param pd the ProtectionDomain associated with the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @return the set of Permissions according to the policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    private PermissionCollection getPermissions(Permissions perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                                        ProtectionDomain pd ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            debug.println("getPermissions:\n\t" + printPD(pd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        final CodeSource cs = pd.getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (cs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        CodeSource canonCodeSource = AccessController.doPrivileged(
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
  1138
            new java.security.PrivilegedAction<>(){
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
  1139
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                public CodeSource run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    return canonicalizeCodebase(cs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        return getPermissions(perms, canonCodeSource, pd.getPrincipals());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * Examines the global policy and returns the provided Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * object with additional permissions granted to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * CodeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1152
     * @param perms the permissions to populate
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1153
     * @param cs the codesource associated with the caller.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * This encapsulates the original location of the code (where the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * came from) and the public key(s) of its signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @return the set of permissions according to the policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    private PermissionCollection getPermissions(Permissions perms,
11992
664aa0d389f9 7147830: NullPointerException in java.security.Policy.implies() when the ProtectionDomain has a null code sou
mullan
parents: 10336
diff changeset
  1160
                                                final CodeSource cs) {
664aa0d389f9 7147830: NullPointerException in java.security.Policy.implies() when the ProtectionDomain has a null code sou
mullan
parents: 10336
diff changeset
  1161
664aa0d389f9 7147830: NullPointerException in java.security.Policy.implies() when the ProtectionDomain has a null code sou
mullan
parents: 10336
diff changeset
  1162
        if (cs == null)
664aa0d389f9 7147830: NullPointerException in java.security.Policy.implies() when the ProtectionDomain has a null code sou
mullan
parents: 10336
diff changeset
  1163
            return perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        CodeSource canonCodeSource = AccessController.doPrivileged(
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
  1166
            new PrivilegedAction<>(){
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
  1167
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                public CodeSource run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    return canonicalizeCodebase(cs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        return getPermissions(perms, canonCodeSource, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    private Permissions getPermissions(Permissions perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                                       final CodeSource cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                                       Principal[] principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        PolicyInfo pi = policyInfo.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        for (PolicyEntry entry : pi.policyEntries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            addPermissions(perms, cs, principals, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    private void addPermissions(Permissions perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        final CodeSource cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        Principal[] principals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        final PolicyEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            debug.println("evaluate codesources:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                "\tPolicy CodeSource: " + entry.getCodeSource() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                "\tActive CodeSource: " + cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        // check to see if the CodeSource implies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        Boolean imp = AccessController.doPrivileged
39884
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
  1201
            (new PrivilegedAction<>() {
9a543219d0bb 8159752: Grant de-privileged module permissions by default with java.security.policy override option
mullan
parents: 38324
diff changeset
  1202
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            public Boolean run() {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22041
diff changeset
  1204
                return entry.getCodeSource().implies(cs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        if (!imp.booleanValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                debug.println("evaluation (codesource) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            // CodeSource does not imply - return and try next policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        // check to see if the Principals imply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        List<PolicyParser.PrincipalEntry> entryPs = entry.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        if (debug != null) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1220
            List<PolicyParser.PrincipalEntry> accPs = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            if (principals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    accPs.add(new PolicyParser.PrincipalEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                                        (principals[i].getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                                        principals[i].getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            debug.println("evaluate principals:\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                "\tPolicy Principals: " + entryPs + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                "\tActive Principals: " + accPs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9771
diff changeset
  1233
        if (entryPs == null || entryPs.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            // policy entry has no principals -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            // add perms regardless of principals in current ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            addPerms(perms, principals, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                debug.println("evaluation (codesource/principals) passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        } else if (principals == null || principals.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            // current thread has no principals but this policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            // has principals - perms are not added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                debug.println("evaluation (principals) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        // current thread has principals and this policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        // has principals.  see if policy entry principals match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // principals in current ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1259
        for (PolicyParser.PrincipalEntry pppe : entryPs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1261
            // Check for wildcards
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1262
            if (pppe.isWildcardClass()) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1263
                // a wildcard class matches all principals in current ACC
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1264
                continue;
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1265
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1267
            if (pppe.isWildcardName()) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1268
                // a wildcard name matches any principal with the same class
16046
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1269
                if (wildcardPrincipalNameImplies(pppe.principalClass,
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1270
                                                 principals)) {
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1271
                    continue;
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1272
                }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1273
                if (debug != null) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1274
                    debug.println("evaluation (principal name wildcard) failed");
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1275
                }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1276
                // policy entry principal not in current ACC -
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1277
                // immediately return and go to next policy entry
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1278
                return;
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1279
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1281
            Set<Principal> pSet = new HashSet<>(Arrays.asList(principals));
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1282
            Subject subject = new Subject(true, pSet,
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1283
                                          Collections.EMPTY_SET,
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1284
                                          Collections.EMPTY_SET);
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1285
            try {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1286
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1287
                Class<?> pClass = Class.forName(pppe.principalClass, false, cl);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1288
                Principal p = getKnownPrincipal(pClass, pppe.principalName);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1289
                if (p == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1290
                    if (!Principal.class.isAssignableFrom(pClass)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1291
                        // not the right subtype
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1292
                        throw new ClassCastException(pppe.principalClass +
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1293
                                                     " is not a Principal");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1294
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1295
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1296
                    Constructor<?> c = pClass.getConstructor(PARAMS1);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1297
                    p = (Principal)c.newInstance(new Object[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1298
                                                 pppe.principalName });
9d0388c6b336 8142968: Module System implementation
alanb
parents: 32834
diff changeset
  1299
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1300
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1302
                if (debug != null) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1303
                    debug.println("found Principal " + p.getClass().getName());
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1304
                }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1305
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1306
                // check if the Principal implies the current
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1307
                // thread's principals
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1308
                if (!p.implies(subject)) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1309
                    if (debug != null) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1310
                        debug.println("evaluation (principal implies) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1313
                    // policy principal does not imply the current Subject -
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1314
                    // immediately return and go to next policy entry
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1315
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            } catch (Exception e) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1318
                // fall back to default principal comparison.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                // see if policy entry principal is in current ACC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1325
                if (!pppe.implies(subject)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    if (debug != null) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1327
                        debug.println("evaluation (default principal implies) failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    // policy entry principal not in current ACC -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    // immediately return and go to next policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            // either the principal information matched,
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1337
            // or the Principal.implies succeeded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            // continue loop and test the next policy principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        // all policy entry principals were found in the current ACC -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        // grant the policy permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            debug.println("evaluation (codesource/principals) passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        addPerms(perms, principals, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
16046
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1350
    /**
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1351
     * Returns true if the array of principals contains at least one
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1352
     * principal of the specified class.
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1353
     */
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1354
    private static boolean wildcardPrincipalNameImplies(String principalClass,
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1355
                                                        Principal[] principals)
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1356
    {
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1357
        for (Principal p : principals) {
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1358
            if (principalClass.equals(p.getClass().getName())) {
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1359
                return true;
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1360
            }
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1361
        }
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1362
        return false;
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1363
    }
c65f0f54851c 8008908: Access denied when invoking Subject.doAsPrivileged()
mullan
parents: 15013
diff changeset
  1364
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    private void addPerms(Permissions perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        Principal[] accPs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                        PolicyEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        for (int i = 0; i < entry.permissions.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            Permission p = entry.permissions.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                debug.println("  granting " + p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if (p instanceof SelfPermission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                // handle "SELF" permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                expandSelf((SelfPermission)p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                        entry.getPrincipals(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                        accPs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                        perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            } else {
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1381
                perms.add(FilePermCompat.newPermPlusAltPath(p));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    /**
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28059
diff changeset
  1387
     * @param sp the SelfPermission that needs to be expanded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * @param entryPs list of principals for the Policy entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * @param pdp Principal array from the current ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * @param perms the PermissionCollection where the individual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *                  Permissions will be added after expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    private void expandSelf(SelfPermission sp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                            List<PolicyParser.PrincipalEntry> entryPs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                            Principal[] pdp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                            Permissions perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9771
diff changeset
  1402
        if (entryPs == null || entryPs.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            // No principals in the grant to substitute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                debug.println("Ignoring permission "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                                + sp.getSelfType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                                + " with target name ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                + sp.getSelfName() + ").  "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                                + "No Principal(s) specified "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                                + "in the grant clause.  "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                                + "SELF-based target names are "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                                + "only valid in the context "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                                + "of a Principal-based grant entry."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                             );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        int startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        int v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        while ((v = sp.getSelfName().indexOf(SELF, startIndex)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            // add non-SELF string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            sb.append(sp.getSelfName().substring(startIndex, v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            // expand SELF
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1427
            Iterator<PolicyParser.PrincipalEntry> pli = entryPs.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            while (pli.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                PolicyParser.PrincipalEntry pppe = pli.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                String[][] principalInfo = getPrincipalInfo(pppe,pdp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                for (int i = 0; i < principalInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                    if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                        sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                    sb.append(principalInfo[i][0] + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                        "\"" + principalInfo[i][1] + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                if (pli.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                    sb.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            startIndex = v + SELF.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        // add remaining string (might be the entire string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        sb.append(sp.getSelfName().substring(startIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            debug.println("  expanded:\n\t" + sp.getSelfName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                        + "\n  into:\n\t" + sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            // first try to instantiate the permission
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1453
            perms.add(FilePermCompat.newPermPlusAltPath(getInstance(sp.getSelfType(),
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1454
                                  sb.toString(),
41377
271ee055cb31 8164705: Remove pathname canonicalization from FilePermission
weijun
parents: 39884
diff changeset
  1455
                                  sp.getSelfActions())));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            // ok, the permission is not in the bootclasspath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            // before we add an UnresolvedPermission, check to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            // whether this perm already belongs to the collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            // if so, use that perm's ClassLoader to create a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            // one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            Class<?> pc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            synchronized (perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                Enumeration<Permission> e = perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    Permission pElement = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    if (pElement.getClass().getName().equals(sp.getSelfType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                        pc = pElement.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            if (pc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                // create an UnresolvedPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                perms.add(new UnresolvedPermission(sp.getSelfType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                                                        sb.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                                                        sp.getSelfActions(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                                                        sp.getCerts()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                    // we found an instantiated permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    // use its class loader to instantiate a new permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                    Constructor<?> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                    // name parameter can not be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                    if (sp.getSelfActions() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                            c = pc.getConstructor(PARAMS1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                            perms.add((Permission)c.newInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                                 (new Object[] {sb.toString()}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                        } catch (NoSuchMethodException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                            c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                            perms.add((Permission)c.newInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                                 (new Object[] {sb.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                                                sp.getSelfActions() }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                        c = pc.getConstructor(PARAMS2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                        perms.add((Permission)c.newInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                           (new Object[] {sb.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                                          sp.getSelfActions()}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                } catch (Exception nme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                        debug.println("self entry expansion " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                        " instantiation failed: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                        +  nme.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                debug.println(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * return the principal class/name pair in the 2D array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * array[x][y]:     x corresponds to the array length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     *                  if (y == 0), it's the principal class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *                  if (y == 1), it's the principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    private String[][] getPrincipalInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        (PolicyParser.PrincipalEntry pe, Principal[] pdp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        // there are 3 possibilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        // 1) the entry's Principal class and name are not wildcarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        // 2) the entry's Principal name is wildcarded only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        // 3) the entry's Principal class and name are wildcarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1531
        if (!pe.isWildcardClass() && !pe.isWildcardName()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            // build an info array for the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            // from the Policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            String[][] info = new String[1][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            info[0][0] = pe.principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            info[0][1] = pe.principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1540
        } else if (!pe.isWildcardClass() && pe.isWildcardName()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            // build an info array for every principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            // in the current domain which has a principal class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            // that is equal to policy entry principal class name
7973
dffe8439eb20 7005608: diamond conversion of JCA and crypto providers
smarks
parents: 7179
diff changeset
  1545
            List<Principal> plist = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            for (int i = 0; i < pdp.length; i++) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1547
                if (pe.principalClass.equals(pdp[i].getClass().getName()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    plist.add(pdp[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            String[][] info = new String[plist.size()][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            int i = 0;
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1552
            for (Principal p : plist) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                info[i][0] = p.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                info[i][1] = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
            // build an info array for every
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            // one of the current Domain's principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            String[][] info = new String[pdp.length][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            for (int i = 0; i < pdp.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                info[i][0] = pdp[i].getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                info[i][1] = pdp[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * Returns the signer certificates from the list of certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * associated with the given code source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * The signer certificates are those certificates that were used
30790
f81f9725a1c6 8065233: Remove Policy provider code that synchronizes on identityPolicyEntries List
mullan
parents: 30374
diff changeset
  1579
     * to verify signed code originating from the codesource location.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * This method assumes that in the given code source, each signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * certificate is followed by its supporting certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * (which may be empty), and that the signer certificate and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * supporting certificate chain are ordered bottom-to-top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * (i.e., with the signer certificate first and the (root) certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * authority last).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    protected Certificate[] getSignerCertificates(CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        Certificate[] certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        if ((certs = cs.getCertificates()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            if (!(certs[i] instanceof X509Certificate))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                return cs.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        // Do we have to do anything?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            while (((i+1) < certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                   && ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                           ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        if (count == certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            // Done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1613
        List<Certificate> userCertList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            userCertList.add(certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            while (((i+1) < certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                   && ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                           ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        Certificate[] userCerts = new Certificate[userCertList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        userCertList.toArray(userCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        return userCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
    private CodeSource canonicalizeCodebase(CodeSource cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                                            boolean extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        String path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        CodeSource canonCs = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        URL u = cs.getLocation();
9771
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1636
        if (u != null) {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1637
            if (u.getProtocol().equals("jar")) {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1638
                // unwrap url embedded inside jar url
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1639
                String spec = u.getFile();
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1640
                int separator = spec.indexOf("!/");
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1641
                if (separator != -1) {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1642
                    try {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1643
                        u = new URL(spec.substring(0, separator));
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1644
                    } catch (MalformedURLException e) {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1645
                        // Fail silently. In this case, url stays what
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1646
                        // it was above
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1647
                    }
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1648
                }
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1649
            }
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1650
            if (u.getProtocol().equals("file")) {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1651
                boolean isLocalFile = false;
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1652
                String host = u.getHost();
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1653
                isLocalFile = (host == null || host.equals("") ||
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1654
                    host.equals("~") || host.equalsIgnoreCase("localhost"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
9771
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1656
                if (isLocalFile) {
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1657
                    path = u.getFile().replace('/', File.separatorChar);
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1658
                    path = ParseUtil.decode(path);
0e8006984450 7044443: Permissions resolved incorrectly for jar protocol (Patch from bugs.openjdk.java.net)
mullan
parents: 9035
diff changeset
  1659
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        if (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                URL csUrl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                path = canonPath(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                csUrl = ParseUtil.fileToEncodedURL(new File(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    canonCs = new CodeSource(csUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                                             getSignerCertificates(cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                    canonCs = new CodeSource(csUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                                             cs.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                // leave codesource as it is, unless we have to extract its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                // signer certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                    canonCs = new CodeSource(cs.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                                             getSignerCertificates(cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                canonCs = new CodeSource(cs.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                                         getSignerCertificates(cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        return canonCs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
4053
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 3956
diff changeset
  1693
    // Wrapper to return a canonical path that avoids calling getCanonicalPath()
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 3956
diff changeset
  1694
    // with paths that are intended to match all entries in the directory
c2f8e57ba2f8 6891707: Eliminate the java.io.FilePermission dependency on PolicyFile
mchung
parents: 3956
diff changeset
  1695
    private static String canonPath(String path) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        if (path.endsWith("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            path = path.substring(0, path.length()-1) + "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            path = new File(path).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            return path.substring(0, path.length()-1) + "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            return new File(path).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    private String printPD(ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        Principal[] principals = pd.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        String pals = "<no principals>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        if (principals != null && principals.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            StringBuilder palBuf = new StringBuilder("(principals ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                palBuf.append(principals[i].getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                              " \"" + principals[i].getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                              "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                if (i < principals.length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    palBuf.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    palBuf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            pals = palBuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        return "PD CodeSource: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                + pd.getCodeSource()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                +"\n\t" + "PD ClassLoader: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                + pd.getClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                +"\n\t" + "PD Principals: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                + pals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * return true if no replacement was performed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * or if replacement succeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    private boolean replacePrincipals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        List<PolicyParser.PrincipalEntry> principals, KeyStore keystore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9771
diff changeset
  1736
        if (principals == null || principals.isEmpty() || keystore == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1739
        for (PolicyParser.PrincipalEntry pppe : principals) {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  1740
            if (pppe.isReplaceName()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                // perform replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                // (only X509 replacement is possible now)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                if ((name = getDN(pppe.principalName, keystore)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                    debug.println("  Replacing \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                        pppe.principalName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                        "\" with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                        X500PRINCIPAL + "/\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                        name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                        "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                pppe.principalClass = X500PRINCIPAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                pppe.principalName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        // return true if no replacement was performed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        // or if replacement succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    private void expandPermissionName(PolicyParser.PermissionEntry pe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                                        KeyStore keystore) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        // short cut the common case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        if (pe.name == null || pe.name.indexOf("${{", 0) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        int startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        int b, e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        while ((b = pe.name.indexOf("${{", startIndex)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
            e = pe.name.indexOf("}}", b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            if (e < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            sb.append(pe.name.substring(startIndex, b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            // get the value in ${{...}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            String value = pe.name.substring(b+3, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            // parse up to the first ':'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            int colonIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
            String prefix = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            String suffix;
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 22041
diff changeset
  1791
            if ((colonIndex = value.indexOf(':')) != -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                prefix = value.substring(0, colonIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            // handle different prefix possibilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            if (prefix.equalsIgnoreCase("self")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                // do nothing - handled later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                sb.append(pe.name.substring(b, e+2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                startIndex = e+2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            } else if (prefix.equalsIgnoreCase("alias")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                // get the suffix and perform keystore alias replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                if (colonIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                    Object[] source = {pe.name};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1805
                    throw new Exception(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1806
                        LocalizedMessage.getMessage(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1807
                            "alias.name.not.provided.pe.name.",
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1808
                            source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                suffix = value.substring(colonIndex+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                if ((suffix = getDN(suffix, keystore)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                    Object[] source = {value.substring(colonIndex+1)};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1813
                    throw new Exception(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1814
                        LocalizedMessage.getMessage(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1815
                            "unable.to.perform.substitution.on.alias.suffix",
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1816
                            source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                sb.append(X500PRINCIPAL + " \"" + suffix + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                startIndex = e+2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                Object[] source = {prefix};
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1823
                throw new Exception(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1824
                    LocalizedMessage.getMessage(
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1825
                        "substitution.value.prefix.unsupported",
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  1826
                        source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        // copy the rest of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        sb.append(pe.name.substring(startIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        // replace the name with expanded value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            debug.println("  Permission name expanded from:\n\t" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                        pe.name + "\nto\n\t" + sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        pe.name = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    private String getDN(String alias, KeyStore keystore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            cert = keystore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                debug.println("  Error retrieving certificate for '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                                "': " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                                e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        if (cert == null || !(cert instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                debug.println("  -- No certificate for '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                                alias +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                                "' - ignoring entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            X509Certificate x509Cert = (X509Certificate)cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            // 4702543:  X500 names with an EmailAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            // were encoded incorrectly.  create new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            // X500Principal name with correct encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            X500Principal p = new X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                (x509Cert.getSubjectX500Principal().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            return p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * Each entry in the policy configuration file is represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * PolicyEntry object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * A PolicyEntry is a (CodeSource,Permission) pair.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * CodeSource contains the (URL, PublicKey) that together identify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * where the Java bytecodes come from and who (if anyone) signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * them.  The URL could refer to localhost.  The URL could also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * null, meaning that this policy entry is given to all comers, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * long as they match the signer field.  The signer could be null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * meaning the code is not signed. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * The Permission contains the (Type, Name, Action) triplet. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * For now, the Policy object retrieves the public key from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * X.509 certificate on disk that corresponds to the signedBy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * alias specified in the Policy config file.  For reasons of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * efficiency, the Policy object keeps a hashtable of certs already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * read in.  This could be replaced by a secure internal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     *          permission java.io.File "/tmp", "read,write",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     *          signedBy "Duke";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * FilePermission f = new FilePermission("/tmp", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * PublicKey p = publickeys.get("Duke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * URL u = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * CodeBase c = new CodeBase( p, u );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * pe = new PolicyEntry(f, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * @see java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * @see java.security.Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    private static class PolicyEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        private final CodeSource codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        final List<Permission> permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        private final List<PolicyParser.PrincipalEntry> principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
         * Given a Permission and a CodeSource, create a policy entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
         * XXX Decide if/how to add validity fields and "purpose" fields to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
         * XXX policy entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
         * @param cs the CodeSource, which encapsulates the URL and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
         *        public key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         *        attributes from the policy config file. Validity checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         *        are performed on the public key before PolicyEntry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         *        called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        PolicyEntry(CodeSource cs, List<PolicyParser.PrincipalEntry> principals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            this.codesource = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            this.permissions = new ArrayList<Permission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            this.principals = principals; // can be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        PolicyEntry(CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            this(cs, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        List<PolicyParser.PrincipalEntry> getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            return principals; // can be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         * add a Permission object to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         * No need to sync add op because perms are added to entry only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         * while entry is being initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        void add(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            permissions.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
         * Return the CodeSource for this policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        CodeSource getCodeSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            return codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  1970
        @Override public String toString(){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
            StringBuilder sb = new StringBuilder();
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1972
            sb.append(ResourcesMgr.getString("LPARAM"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            sb.append(getCodeSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            sb.append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            for (int j = 0; j < permissions.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                Permission p = permissions.get(j);
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1977
                sb.append(ResourcesMgr.getString("SPACE"));
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1978
                sb.append(ResourcesMgr.getString("SPACE"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                sb.append(p);
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1980
                sb.append(ResourcesMgr.getString("NEWLINE"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            }
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1982
            sb.append(ResourcesMgr.getString("RPARAM"));
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
  1983
            sb.append(ResourcesMgr.getString("NEWLINE"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    private static class SelfPermission extends Permission {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        private static final long serialVersionUID = -8315562579967246806L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         * The class name of the Permission class that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * created when this self permission is expanded .
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        private String type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
         * The permission name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         * The actions of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        private String actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
         * The certs of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
         * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
         */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30790
diff changeset
  2019
        private Certificate[] certs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         * Creates a new SelfPermission containing the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         * information needed later to expand the self
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
         * @param type the class name of the Permission class that will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
         * created when this permission is expanded and if necessary resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
         * @param name the name of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
         * @param actions the actions of the permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
         * @param certs the certificates the permission's class was signed with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
         * This is a list of certificate chains, where each chain is composed of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
         * a signer certificate and optionally its supporting certificate chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
         * Each chain is ordered bottom-to-top (i.e., with the signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
         * certificate first and the (root) certificate authority last).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        public SelfPermission(String type, String name, String actions,
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30790
diff changeset
  2035
                              Certificate[] certs)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            super(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                throw new NullPointerException
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 41377
diff changeset
  2040
                    (LocalizedMessage.getMessage("type.can.t.be.null"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            this.actions = actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                // Extract the signer certs from the list of certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                    if (!(certs[i] instanceof X509Certificate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                        // there is no concept of signer certs, so we store the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                        // entire cert array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                        this.certs = certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                if (this.certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                    // Go through the list of certs and see if all the certs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                    // signer certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                    int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                    while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                        while (((i+1) < certs.length) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                            ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                            ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                    if (count == certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                        // All the certs are signer certs, so we store the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                        // entire array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                        this.certs = certs.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                    if (this.certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                        // extract the signer certs
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  2078
                        List<Certificate> signerCerts = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                        while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                            signerCerts.add(certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                            while (((i+1) < certs.length) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                                ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                                ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                        this.certs = new Certificate[signerCerts.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                        signerCerts.toArray(this.certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
         * This method always returns false for SelfPermission permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
         * That is, an SelfPermission never considered to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
         * imply another permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
         * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
         * @return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2105
        @Override public boolean implies(Permission p) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
         * Checks two SelfPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
         * Checks that <i>obj</i> is an SelfPermission, and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
         * the same type (class) name, permission name, actions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
         * certificates as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
         * @param obj the object we are testing for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
         * @return true if obj is an SelfPermission, and has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
         * type (class) name, permission name, actions, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
         * certificates as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2122
        @Override public boolean equals(Object obj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
            if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            if (! (obj instanceof SelfPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            SelfPermission that = (SelfPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            if (!(this.type.equals(that.type) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                this.name.equals(that.name) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                this.actions.equals(that.actions)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            if (this.certs.length != that.certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
            int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
            for (i = 0; i < this.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                for (j = 0; j < that.certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                    if (this.certs[i].equals(that.certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            for (i = 0; i < that.certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                for (j = 0; j < this.certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                    if (that.certs[i].equals(this.certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                        match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
         * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
         * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2170
        @Override public int hashCode() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            int hash = type.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                hash ^= name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            if (actions != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                hash ^= actions.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
            return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
         * Returns the canonical string representation of the actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
         * which currently is the empty string "", since there are no actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
         * for an SelfPermission. That is, the actions for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         * permission that will be created when this SelfPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
         * is resolved may be non-null, but an SelfPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
         * itself is never considered to have any actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
         * @return the empty string "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2189
        @Override public String getActions() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
        public String getSelfType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        public String getSelfName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        public String getSelfActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        public Certificate[] getCerts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
         * Returns a string describing this SelfPermission.  The convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
         * is to specify the class name, the permission name, and the actions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
         * in the following format: '(unresolved "ClassName" "name" "actions")'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
         * @return information about this SelfPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
         */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2216
        @Override public String toString() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            return "(SelfPermission " + type + " " + name + " " + actions + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * holds policy information that we need to synch on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    private static class PolicyInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        private static final boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        // Stores grant entries in the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        final List<PolicyEntry> policyEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        // Maps aliases to certs
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9771
diff changeset
  2231
        final Map<Object, Object> aliasMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        // Maps ProtectionDomain to PermissionCollection
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2234
        private final ProtectionDomainCache[] pdMapping;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        private java.util.Random random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        PolicyInfo(int numCaches) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  2238
            policyEntries = new ArrayList<>();
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 14342
diff changeset
  2239
            aliasMapping = Collections.synchronizedMap(new HashMap<>(11));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2241
            pdMapping = new ProtectionDomainCache[numCaches];
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2242
            JavaSecurityProtectionDomainAccess jspda
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2243
                = SharedSecrets.getJavaSecurityProtectionDomainAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            for (int i = 0; i < numCaches; i++) {
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2245
                pdMapping[i] = jspda.getProtectionDomainCache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            if (numCaches > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                random = new java.util.Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 4053
diff changeset
  2251
        ProtectionDomainCache getPdMapping() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            if (pdMapping.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                return pdMapping[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                int i = java.lang.Math.abs(random.nextInt() % pdMapping.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                return pdMapping[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
}