src/java.base/share/classes/sun/security/provider/SubjectCodeSource.java
author darcy
Wed, 09 Oct 2019 09:57:41 -0700
changeset 58519 6e017b301287
parent 57950 4612a3cfb927
child 58679 9c3209ff7550
permissions -rw-r--r--
8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes Reviewed-by: mullan, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47478
diff changeset
     2
 * Copyright (c) 1999, 2019, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
    26
package sun.security.provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.Subject;
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
    36
import sun.security.provider.PolicyParser.PrincipalEntry;
43243
a48dab17a356 8173024: Replace direct use of AuthResources resource bundle from jdk.security.auth
mchung
parents: 25859
diff changeset
    37
import sun.security.util.ResourcesMgr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> This <code>SubjectCodeSource</code> class contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * a <code>URL</code>, signer certificates, and either a <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * (that represents the <code>Subject</code> in the current
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
    43
 * <code>AccessControlContext</code>), or a linked list of Principals
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * (that represent a "subject" in a <code>Policy</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class SubjectCodeSource extends CodeSource implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 47478
diff changeset
    49
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final long serialVersionUID = 6039418085604715275L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Subject subject;
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
    53
    private LinkedList<PrincipalEntry> principals;
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 19439
diff changeset
    54
    private static final Class<?>[] PARAMS = { String.class };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        sun.security.util.Debug.getInstance("auth", "\t[Auth Access]");
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
    57
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private ClassLoader sysClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Creates a new <code>SubjectCodeSource</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * with the given <code>Subject</code>, principals, <code>URL</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * and signers (Certificates).  The <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * represents the <code>Subject</code> associated with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The Principals are given as a <code>LinkedList</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * of <code>PolicyParser.PrincipalEntry</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Typically either a <code>Subject</code> will be provided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * or a list of <code>principals</code> will be provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * (not both).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param subject the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *                  <code>SubjectCodeSource</code> <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param url the <code>URL</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *                  <code>SubjectCodeSource</code> <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param certs the signers associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *                  <code>SubjectCodeSource</code> <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    SubjectCodeSource(Subject subject,
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
    84
        LinkedList<PrincipalEntry> principals,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        URL url, Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        super(url, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.principals = (principals == null ?
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
    90
                new LinkedList<PrincipalEntry>() :
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
    91
                new LinkedList<PrincipalEntry>(principals));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        sysClassLoader = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        (new java.security.PrivilegedAction<ClassLoader>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            public ClassLoader run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    return ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Get the Principals associated with this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The Principals are retrieved as a <code>LinkedList</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * of <code>PolicyParser.PrincipalEntry</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return the Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          <code>SubjectCodeSource</code> as a <code>LinkedList</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *          of <code>PolicyParser.PrincipalEntry</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   111
    LinkedList<PrincipalEntry> getPrincipals() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Get the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>SubjectCodeSource</code>.  The <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * represents the <code>Subject</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * current <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *          <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    Subject getSubject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Returns true if this <code>SubjectCodeSource</code> object "implies"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * the specified <code>CodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * More specifically, this method makes the following checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * If any fail, it returns false.  If they all succeed, it returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <li> The provided codesource must not be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <li> codesource must be an instance of <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <li> super.implies(codesource) must return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <li> for each principal in this codesource's principal list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <li>     if the principal is an instanceof
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   144
     *          <code>Principal</code>, then the principal must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *          imply the provided codesource's <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <li>     if the principal is not an instanceof
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   147
     *          <code>Principal</code>, then the provided
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *          codesource's <code>Subject</code> must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          associated <code>Principal</code>, <i>P</i>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          P.getClass().getName equals principal.principalClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *          and P.getName() equals principal.principalName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param codesource the <code>CodeSource</code> to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
   159
     * @return true if this <code>SubjectCodeSource</code> implies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          the specified <code>CodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public boolean implies(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   164
        LinkedList<PrincipalEntry> subjectList = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (codesource == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            !(codesource instanceof SubjectCodeSource) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            !(super.implies(codesource))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                debug.println("\tSubjectCodeSource.implies: FAILURE 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        SubjectCodeSource that = (SubjectCodeSource)codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // if the principal list in the policy "implies"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // the Subject associated with the current AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // then return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (this.principals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                debug.println("\tSubjectCodeSource.implies: PASS 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (that.getSubject() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            that.getSubject().getPrincipals().size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                debug.println("\tSubjectCodeSource.implies: FAILURE 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   194
        ListIterator<PrincipalEntry> li = this.principals.listIterator(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        while (li.hasNext()) {
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   196
            PrincipalEntry pppe = li.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   199
                // use new Principal.implies method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   201
                Class<?> pClass = Class.forName(pppe.principalClass,
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   202
                                                true, sysClassLoader);
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   203
                if (!Principal.class.isAssignableFrom(pClass)) {
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   204
                    // not the right subtype
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   205
                    throw new ClassCastException(pppe.principalClass +
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   206
                                                 " is not a Principal");
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   207
                }
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   208
                Constructor<?> c = pClass.getConstructor(PARAMS);
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   209
                Principal p = (Principal)c.newInstance(new Object[] {
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   210
                                                       pppe.principalName });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   212
                if (!p.implies(that.getSubject())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        debug.println("\tSubjectCodeSource.implies: FAILURE 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        debug.println("\tSubjectCodeSource.implies: PASS 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
19439
57876ed3c426 8016850: JCK javax.security.auth.Policy tests fail when run in Profiles mode
mullan
parents: 19212
diff changeset
   223
                // simply compare Principals
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                if (subjectList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    if (that.getSubject() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            debug.println("\tSubjectCodeSource.implies: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        "FAILURE 4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    Iterator<Principal> i =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                that.getSubject().getPrincipals().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   236
                    subjectList = new LinkedList<PrincipalEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        Principal p = i.next();
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   239
                        PrincipalEntry spppe = new PrincipalEntry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                (p.getClass().getName(), p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        subjectList.add(spppe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (!subjectListImpliesPrincipalEntry(subjectList, pppe)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        debug.println("\tSubjectCodeSource.implies: FAILURE 5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            debug.println("\tSubjectCodeSource.implies: PASS 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * This method returns, true, if the provided <i>subjectList</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * "contains" the <code>Principal</code> specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * in the provided <i>pppe</i> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Note that the provided <i>pppe</i> argument may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * wildcards (*) for the <code>Principal</code> class and name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * which need to be considered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param subjectList a list of PolicyParser.PrincipalEntry objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          that correspond to all the Principals in the Subject currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          on this thread's AccessControlContext. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param pppe the Principals specified in a grant entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return true if the provided <i>subjectList</i> "contains"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          the <code>Principal</code> specified in the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *          <i>pppe</i> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private boolean subjectListImpliesPrincipalEntry(
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   280
                LinkedList<PrincipalEntry> subjectList, PrincipalEntry pppe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   282
        ListIterator<PrincipalEntry> li = subjectList.listIterator(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        while (li.hasNext()) {
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   284
            PrincipalEntry listPppe = li.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   286
            if (pppe.getPrincipalClass().equals
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   287
                        (PrincipalEntry.WILDCARD_CLASS) ||
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   288
                pppe.getPrincipalClass().equals(listPppe.getPrincipalClass()))
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   289
            {
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   290
                if (pppe.getPrincipalName().equals
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   291
                        (PrincipalEntry.WILDCARD_NAME) ||
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   292
                    pppe.getPrincipalName().equals(listPppe.getPrincipalName()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * object. Two <code>SubjectCodeSource</code> objects are considered equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * if their locations are of identical value, if the two sets of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Certificates are of identical values, and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Subjects are equal, and if the PolicyParser.PrincipalEntry values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * are of identical values.  It is not required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * the Certificates or PolicyParser.PrincipalEntry values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * be in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return true if the objects are considered equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (super.equals(obj) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (!(obj instanceof SubjectCodeSource))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        SubjectCodeSource that = (SubjectCodeSource)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // the principal lists must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (this.getSubject() != that.getSubject())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if ((this.principals == null && that.principals != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            (this.principals != null && that.principals == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (this.principals != null && that.principals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (!this.principals.containsAll(that.principals) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                !that.principals.containsAll(this.principals))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Return a hashcode for this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return a hashcode for this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Return a String representation of this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @return a String representation of this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        String returnMe = super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (getSubject() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                final Subject finalSubject = getSubject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                returnMe = returnMe + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                (new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                    return finalSubject.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                returnMe = returnMe + "\n" + getSubject().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (principals != null) {
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   385
            ListIterator<PrincipalEntry> li = principals.listIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            while (li.hasNext()) {
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   387
                PrincipalEntry pppe = li.next();
43243
a48dab17a356 8173024: Replace direct use of AuthResources resource bundle from jdk.security.auth
mchung
parents: 25859
diff changeset
   388
                returnMe = returnMe + ResourcesMgr.getAuthResourceString("NEWLINE") +
19212
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   389
                        pppe.getPrincipalClass() + " " +
80526b520497 8022410: Fix Javac Warnings in com.sun.security.auth Package
dxu
parents: 10336
diff changeset
   390
                        pppe.getPrincipalName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return returnMe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}