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