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