aboutsummaryrefslogtreecommitdiff
path: root/src/java.base/share/classes/sun/security/provider/SubjectCodeSource.java
blob: cf22b58ba4c72681ca2335f530c45180be296777 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.security.provider;

import java.net.URL;
import java.util.*;
import java.security.CodeSource;
import java.security.Principal;
import java.security.cert.Certificate;
import java.lang.reflect.Constructor;

import javax.security.auth.Subject;
import sun.security.provider.PolicyParser.PrincipalEntry;
import sun.security.util.ResourcesMgr;

/**
 * <p> This <code>SubjectCodeSource</code> class contains
 * a <code>URL</code>, signer certificates, and either a <code>Subject</code>
 * (that represents the <code>Subject</code> in the current
 * <code>AccessControlContext</code>), or a linked list of Principals
 * (that represent a "subject" in a <code>Policy</code>).
 *
 */
class SubjectCodeSource extends CodeSource implements java.io.Serializable {

    @java.io.Serial
    private static final long serialVersionUID = 6039418085604715275L;

    private Subject subject;
    private LinkedList<PrincipalEntry> principals;
    private static final Class<?>[] PARAMS = { String.class };
    private static final sun.security.util.Debug debug =
        sun.security.util.Debug.getInstance("auth", "\t[Auth Access]");
    @SuppressWarnings("serial") // Not statically typed as Serializable
    private ClassLoader sysClassLoader;

    /**
     * Creates a new <code>SubjectCodeSource</code>
     * with the given <code>Subject</code>, principals, <code>URL</code>,
     * and signers (Certificates).  The <code>Subject</code>
     * represents the <code>Subject</code> associated with the current
     * <code>AccessControlContext</code>.
     * The Principals are given as a <code>LinkedList</code>
     * of <code>PolicyParser.PrincipalEntry</code> objects.
     * Typically either a <code>Subject</code> will be provided,
     * or a list of <code>principals</code> will be provided
     * (not both).
     *
     * <p>
     *
     * @param subject the <code>Subject</code> associated with this
     *                  <code>SubjectCodeSource</code> <p>
     *
     * @param url the <code>URL</code> associated with this
     *                  <code>SubjectCodeSource</code> <p>
     *
     * @param certs the signers associated with this
     *                  <code>SubjectCodeSource</code> <p>
     */
    @SuppressWarnings("removal")
    SubjectCodeSource(Subject subject,
        LinkedList<PrincipalEntry> principals,
        URL url, Certificate[] certs) {

        super(url, certs);
        this.subject = subject;
        this.principals = (principals == null ?
                new LinkedList<PrincipalEntry>() :
                new LinkedList<PrincipalEntry>(principals));
        sysClassLoader = java.security.AccessController.doPrivileged
        (new java.security.PrivilegedAction<ClassLoader>() {
            public ClassLoader run() {
                    return ClassLoader.getSystemClassLoader();
            }
        });
    }

    /**
     * Get the Principals associated with this <code>SubjectCodeSource</code>.
     * The Principals are retrieved as a <code>LinkedList</code>
     * of <code>PolicyParser.PrincipalEntry</code> objects.
     *
     * <p>
     *
     * @return the Principals associated with this
     *          <code>SubjectCodeSource</code> as a <code>LinkedList</code>
     *          of <code>PolicyParser.PrincipalEntry</code> objects.
     */
    LinkedList<PrincipalEntry> getPrincipals() {
        return principals;
    }

    /**
     * Get the <code>Subject</code> associated with this
     * <code>SubjectCodeSource</code>.  The <code>Subject</code>
     * represents the <code>Subject</code> associated with the
     * current <code>AccessControlContext</code>.
     *
     * <p>
     *
     * @return the <code>Subject</code> associated with this
     *          <code>SubjectCodeSource</code>.
     */
    Subject getSubject() {
        return subject;
    }

    /**
     * Returns true if this <code>SubjectCodeSource</code> object "implies"
     * the specified <code>CodeSource</code>.
     * More specifically, this method makes the following checks.
     * If any fail, it returns false.  If they all succeed, it returns true.
     *
     * <p>
     * <ol>
     * <li> The provided codesource must not be <code>null</code>.
     * <li> codesource must be an instance of <code>SubjectCodeSource</code>.
     * <li> super.implies(codesource) must return true.
     * <li> for each principal in this codesource's principal list:
     * <ol>
     * <li>     if the principal is an instanceof
     *          <code>Principal</code>, then the principal must
     *          imply the provided codesource's <code>Subject</code>.
     * <li>     if the principal is not an instanceof
     *          <code>Principal</code>, then the provided
     *          codesource's <code>Subject</code> must have an
     *          associated <code>Principal</code>, <i>P</i>, where
     *          P.getClass().getName equals principal.principalClass,
     *          and P.getName() equals principal.principalName.
     * </ol>
     * </ol>
     *
     * <p>
     *
     * @param codesource the <code>CodeSource</code> to compare against.
     *
     * @return true if this <code>SubjectCodeSource</code> implies
     *          the specified <code>CodeSource</code>.
     */
    public boolean implies(CodeSource codesource) {

        LinkedList<PrincipalEntry> subjectList = null;

        if (codesource == null ||
            !(codesource instanceof SubjectCodeSource) ||
            !(super.implies(codesource))) {

            if (debug != null)
                debug.println("\tSubjectCodeSource.implies: FAILURE 1");
            return false;
        }

        SubjectCodeSource that = (SubjectCodeSource)codesource;

        // if the principal list in the policy "implies"
        // the Subject associated with the current AccessControlContext,
        // then return true

        if (this.principals == null) {
            if (debug != null)
                debug.println("\tSubjectCodeSource.implies: PASS 1");
            return true;
        }

        if (that.getSubject() == null ||
            that.getSubject().getPrincipals().size() == 0) {
            if (debug != null)
                debug.println("\tSubjectCodeSource.implies: FAILURE 2");
            return false;
        }

        ListIterator<PrincipalEntry> li = this.principals.listIterator(0);
        while (li.hasNext()) {
            PrincipalEntry pppe = li.next();
            try {

                // use new Principal.implies method

                Class<?> pClass = Class.forName(pppe.principalClass,
                                                true, sysClassLoader);
                if (!Principal.class.isAssignableFrom(pClass)) {
                    // not the right subtype
                    throw new ClassCastException(pppe.principalClass +
                                                 " is not a Principal");
                }
                Constructor<?> c = pClass.getConstructor(PARAMS);
                Principal p = (Principal)c.newInstance(new Object[] {
                                                       pppe.principalName });

                if (!p.implies(that.getSubject())) {
                    if (debug != null)
                        debug.println("\tSubjectCodeSource.implies: FAILURE 3");
                    return false;
                } else {
                    if (debug != null)
                        debug.println("\tSubjectCodeSource.implies: PASS 2");
                    return true;
                }
            } catch (Exception e) {

                // simply compare Principals

                if (subjectList == null) {

                    if (that.getSubject() == null) {
                        if (debug != null)
                            debug.println("\tSubjectCodeSource.implies: " +
                                        "FAILURE 4");
                        return false;
                    }
                    Iterator<Principal> i =
                                that.getSubject().getPrincipals().iterator();

                    subjectList = new LinkedList<PrincipalEntry>();
                    while (i.hasNext()) {
                        Principal p = i.next();
                        PrincipalEntry spppe = new PrincipalEntry
                                (p.getClass().getName(), p.getName());
                        subjectList.add(spppe);
                    }
                }

                if (!subjectListImpliesPrincipalEntry(subjectList, pppe)) {
                    if (debug != null)
                        debug.println("\tSubjectCodeSource.implies: FAILURE 5");
                    return false;
                }
            }
        }

        if (debug != null)
            debug.println("\tSubjectCodeSource.implies: PASS 3");
        return true;
    }

    /**
     * This method returns, true, if the provided <i>subjectList</i>
     * "contains" the <code>Principal</code> specified
     * in the provided <i>pppe</i> argument.
     *
     * Note that the provided <i>pppe</i> argument may have
     * wildcards (*) for the <code>Principal</code> class and name,
     * which need to be considered.
     *
     * <p>
     *
     * @param subjectList a list of PolicyParser.PrincipalEntry objects
     *          that correspond to all the Principals in the Subject currently
     *          on this thread's AccessControlContext. <p>
     *
     * @param pppe the Principals specified in a grant entry.
     *
     * @return true if the provided <i>subjectList</i> "contains"
     *          the <code>Principal</code> specified in the provided
     *          <i>pppe</i> argument.
     */
    private boolean subjectListImpliesPrincipalEntry(
                LinkedList<PrincipalEntry> subjectList, PrincipalEntry pppe) {

        ListIterator<PrincipalEntry> li = subjectList.listIterator(0);
        while (li.hasNext()) {
            PrincipalEntry listPppe = li.next();

            if (pppe.getPrincipalClass().equals
                        (PrincipalEntry.WILDCARD_CLASS) ||
                pppe.getPrincipalClass().equals(listPppe.getPrincipalClass()))
            {
                if (pppe.getPrincipalName().equals
                        (PrincipalEntry.WILDCARD_NAME) ||
                    pppe.getPrincipalName().equals(listPppe.getPrincipalName()))
                    return true;
            }
        }
        return false;
    }

    /**
     * Tests for equality between the specified object and this
     * object. Two <code>SubjectCodeSource</code> objects are considered equal
     * if their locations are of identical value, if the two sets of
     * Certificates are of identical values, and if the
     * Subjects are equal, and if the PolicyParser.PrincipalEntry values
     * are of identical values.  It is not required that
     * the Certificates or PolicyParser.PrincipalEntry values
     * be in the same order.
     *
     * <p>
     *
     * @param obj the object to test for equality with this object.
     *
     * @return true if the objects are considered equal, false otherwise.
     */
    public boolean equals(Object obj) {

        if (obj == this)
            return true;

        if (super.equals(obj) == false)
            return false;

        if (!(obj instanceof SubjectCodeSource))
            return false;

        SubjectCodeSource that = (SubjectCodeSource)obj;

        // the principal lists must match
        try {
            if (this.getSubject() != that.getSubject())
                return false;
        } catch (SecurityException se) {
            return false;
        }

        if ((this.principals == null && that.principals != null) ||
            (this.principals != null && that.principals == null))
            return false;

        if (this.principals != null && that.principals != null) {
            if (!this.principals.containsAll(that.principals) ||
                !that.principals.containsAll(this.principals))

                return false;
        }

        return true;
    }

    /**
     * Return a hashcode for this <code>SubjectCodeSource</code>.
     *
     * <p>
     *
     * @return a hashcode for this <code>SubjectCodeSource</code>.
     */
    public int hashCode() {
        return super.hashCode();
    }

    /**
     * Return a String representation of this <code>SubjectCodeSource</code>.
     *
     * <p>
     *
     * @return a String representation of this <code>SubjectCodeSource</code>.
     */
    @SuppressWarnings("removal")
    public String toString() {
        String returnMe = super.toString();
        if (getSubject() != null) {
            if (debug != null) {
                final Subject finalSubject = getSubject();
                returnMe = returnMe + "\n" +
                        java.security.AccessController.doPrivileged
                                (new java.security.PrivilegedAction<String>() {
                                public String run() {
                                    return finalSubject.toString();
                                }
                        });
            } else {
                returnMe = returnMe + "\n" + getSubject().toString();
            }
        }
        if (principals != null) {
            ListIterator<PrincipalEntry> li = principals.listIterator();
            while (li.hasNext()) {
                PrincipalEntry pppe = li.next();
                returnMe = returnMe + ResourcesMgr.getAuthResourceString("NEWLINE") +
                        pppe.getPrincipalClass() + " " +
                        pppe.getPrincipalName();
            }
        }
        return returnMe;
    }
}