Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/provider/SubjectCodeSource.java
38830 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import java.net.URL;28import java.util.*;29import java.security.CodeSource;30import java.security.Principal;31import java.security.cert.Certificate;32import java.lang.reflect.Constructor;3334import javax.security.auth.Subject;35import sun.security.provider.PolicyParser.PrincipalEntry;3637/**38* <p> This <code>SubjectCodeSource</code> class contains39* a <code>URL</code>, signer certificates, and either a <code>Subject</code>40* (that represents the <code>Subject</code> in the current41* <code>AccessControlContext</code>), or a linked list of Principals42* (that represent a "subject" in a <code>Policy</code>).43*44*/45class SubjectCodeSource extends CodeSource implements java.io.Serializable {4647private static final long serialVersionUID = 6039418085604715275L;4849private static final java.util.ResourceBundle rb =50java.security.AccessController.doPrivileged51(new java.security.PrivilegedAction<java.util.ResourceBundle>() {52public java.util.ResourceBundle run() {53return (java.util.ResourceBundle.getBundle54("sun.security.util.AuthResources"));55}56});5758private Subject subject;59private LinkedList<PrincipalEntry> principals;60private static final Class<?>[] PARAMS = { String.class };61private static final sun.security.util.Debug debug =62sun.security.util.Debug.getInstance("auth", "\t[Auth Access]");63private ClassLoader sysClassLoader;6465/**66* Creates a new <code>SubjectCodeSource</code>67* with the given <code>Subject</code>, principals, <code>URL</code>,68* and signers (Certificates). The <code>Subject</code>69* represents the <code>Subject</code> associated with the current70* <code>AccessControlContext</code>.71* The Principals are given as a <code>LinkedList</code>72* of <code>PolicyParser.PrincipalEntry</code> objects.73* Typically either a <code>Subject</code> will be provided,74* or a list of <code>principals</code> will be provided75* (not both).76*77* <p>78*79* @param subject the <code>Subject</code> associated with this80* <code>SubjectCodeSource</code> <p>81*82* @param url the <code>URL</code> associated with this83* <code>SubjectCodeSource</code> <p>84*85* @param certs the signers associated with this86* <code>SubjectCodeSource</code> <p>87*/88SubjectCodeSource(Subject subject,89LinkedList<PrincipalEntry> principals,90URL url, Certificate[] certs) {9192super(url, certs);93this.subject = subject;94this.principals = (principals == null ?95new LinkedList<PrincipalEntry>() :96new LinkedList<PrincipalEntry>(principals));97sysClassLoader = java.security.AccessController.doPrivileged98(new java.security.PrivilegedAction<ClassLoader>() {99public ClassLoader run() {100return ClassLoader.getSystemClassLoader();101}102});103}104105/**106* Get the Principals associated with this <code>SubjectCodeSource</code>.107* The Principals are retrieved as a <code>LinkedList</code>108* of <code>PolicyParser.PrincipalEntry</code> objects.109*110* <p>111*112* @return the Principals associated with this113* <code>SubjectCodeSource</code> as a <code>LinkedList</code>114* of <code>PolicyParser.PrincipalEntry</code> objects.115*/116LinkedList<PrincipalEntry> getPrincipals() {117return principals;118}119120/**121* Get the <code>Subject</code> associated with this122* <code>SubjectCodeSource</code>. The <code>Subject</code>123* represents the <code>Subject</code> associated with the124* current <code>AccessControlContext</code>.125*126* <p>127*128* @return the <code>Subject</code> associated with this129* <code>SubjectCodeSource</code>.130*/131Subject getSubject() {132return subject;133}134135/**136* Returns true if this <code>SubjectCodeSource</code> object "implies"137* the specified <code>CodeSource</code>.138* More specifically, this method makes the following checks.139* If any fail, it returns false. If they all succeed, it returns true.140*141* <p>142* <ol>143* <li> The provided codesource must not be <code>null</code>.144* <li> codesource must be an instance of <code>SubjectCodeSource</code>.145* <li> super.implies(codesource) must return true.146* <li> for each principal in this codesource's principal list:147* <ol>148* <li> if the principal is an instanceof149* <code>Principal</code>, then the principal must150* imply the provided codesource's <code>Subject</code>.151* <li> if the principal is not an instanceof152* <code>Principal</code>, then the provided153* codesource's <code>Subject</code> must have an154* associated <code>Principal</code>, <i>P</i>, where155* P.getClass().getName equals principal.principalClass,156* and P.getName() equals principal.principalName.157* </ol>158* </ol>159*160* <p>161*162* @param codesource the <code>CodeSource</code> to compare against.163*164* @return true if this <code>SubjectCodeSource</code> implies the165* the specified <code>CodeSource</code>.166*/167public boolean implies(CodeSource codesource) {168169LinkedList<PrincipalEntry> subjectList = null;170171if (codesource == null ||172!(codesource instanceof SubjectCodeSource) ||173!(super.implies(codesource))) {174175if (debug != null)176debug.println("\tSubjectCodeSource.implies: FAILURE 1");177return false;178}179180SubjectCodeSource that = (SubjectCodeSource)codesource;181182// if the principal list in the policy "implies"183// the Subject associated with the current AccessControlContext,184// then return true185186if (this.principals == null) {187if (debug != null)188debug.println("\tSubjectCodeSource.implies: PASS 1");189return true;190}191192if (that.getSubject() == null ||193that.getSubject().getPrincipals().size() == 0) {194if (debug != null)195debug.println("\tSubjectCodeSource.implies: FAILURE 2");196return false;197}198199ListIterator<PrincipalEntry> li = this.principals.listIterator(0);200while (li.hasNext()) {201PrincipalEntry pppe = li.next();202try {203204// use new Principal.implies method205206Class<?> pClass = Class.forName(pppe.principalClass,207true, sysClassLoader);208if (!Principal.class.isAssignableFrom(pClass)) {209// not the right subtype210throw new ClassCastException(pppe.principalClass +211" is not a Principal");212}213Constructor<?> c = pClass.getConstructor(PARAMS);214Principal p = (Principal)c.newInstance(new Object[] {215pppe.principalName });216217if (!p.implies(that.getSubject())) {218if (debug != null)219debug.println("\tSubjectCodeSource.implies: FAILURE 3");220return false;221} else {222if (debug != null)223debug.println("\tSubjectCodeSource.implies: PASS 2");224return true;225}226} catch (Exception e) {227228// simply compare Principals229230if (subjectList == null) {231232if (that.getSubject() == null) {233if (debug != null)234debug.println("\tSubjectCodeSource.implies: " +235"FAILURE 4");236return false;237}238Iterator<Principal> i =239that.getSubject().getPrincipals().iterator();240241subjectList = new LinkedList<PrincipalEntry>();242while (i.hasNext()) {243Principal p = i.next();244PrincipalEntry spppe = new PrincipalEntry245(p.getClass().getName(), p.getName());246subjectList.add(spppe);247}248}249250if (!subjectListImpliesPrincipalEntry(subjectList, pppe)) {251if (debug != null)252debug.println("\tSubjectCodeSource.implies: FAILURE 5");253return false;254}255}256}257258if (debug != null)259debug.println("\tSubjectCodeSource.implies: PASS 3");260return true;261}262263/**264* This method returns, true, if the provided <i>subjectList</i>265* "contains" the <code>Principal</code> specified266* in the provided <i>pppe</i> argument.267*268* Note that the provided <i>pppe</i> argument may have269* wildcards (*) for the <code>Principal</code> class and name,270* which need to be considered.271*272* <p>273*274* @param subjectList a list of PolicyParser.PrincipalEntry objects275* that correspond to all the Principals in the Subject currently276* on this thread's AccessControlContext. <p>277*278* @param pppe the Principals specified in a grant entry.279*280* @return true if the provided <i>subjectList</i> "contains"281* the <code>Principal</code> specified in the provided282* <i>pppe</i> argument.283*/284private boolean subjectListImpliesPrincipalEntry(285LinkedList<PrincipalEntry> subjectList, PrincipalEntry pppe) {286287ListIterator<PrincipalEntry> li = subjectList.listIterator(0);288while (li.hasNext()) {289PrincipalEntry listPppe = li.next();290291if (pppe.getPrincipalClass().equals292(PrincipalEntry.WILDCARD_CLASS) ||293pppe.getPrincipalClass().equals(listPppe.getPrincipalClass()))294{295if (pppe.getPrincipalName().equals296(PrincipalEntry.WILDCARD_NAME) ||297pppe.getPrincipalName().equals(listPppe.getPrincipalName()))298return true;299}300}301return false;302}303304/**305* Tests for equality between the specified object and this306* object. Two <code>SubjectCodeSource</code> objects are considered equal307* if their locations are of identical value, if the two sets of308* Certificates are of identical values, and if the309* Subjects are equal, and if the PolicyParser.PrincipalEntry values310* are of identical values. It is not required that311* the Certificates or PolicyParser.PrincipalEntry values312* be in the same order.313*314* <p>315*316* @param obj the object to test for equality with this object.317*318* @return true if the objects are considered equal, false otherwise.319*/320public boolean equals(Object obj) {321322if (obj == this)323return true;324325if (super.equals(obj) == false)326return false;327328if (!(obj instanceof SubjectCodeSource))329return false;330331SubjectCodeSource that = (SubjectCodeSource)obj;332333// the principal lists must match334try {335if (this.getSubject() != that.getSubject())336return false;337} catch (SecurityException se) {338return false;339}340341if ((this.principals == null && that.principals != null) ||342(this.principals != null && that.principals == null))343return false;344345if (this.principals != null && that.principals != null) {346if (!this.principals.containsAll(that.principals) ||347!that.principals.containsAll(this.principals))348349return false;350}351352return true;353}354355/**356* Return a hashcode for this <code>SubjectCodeSource</code>.357*358* <p>359*360* @return a hashcode for this <code>SubjectCodeSource</code>.361*/362public int hashCode() {363return super.hashCode();364}365366/**367* Return a String representation of this <code>SubjectCodeSource</code>.368*369* <p>370*371* @return a String representation of this <code>SubjectCodeSource</code>.372*/373public String toString() {374String returnMe = super.toString();375if (getSubject() != null) {376if (debug != null) {377final Subject finalSubject = getSubject();378returnMe = returnMe + "\n" +379java.security.AccessController.doPrivileged380(new java.security.PrivilegedAction<String>() {381public String run() {382return finalSubject.toString();383}384});385} else {386returnMe = returnMe + "\n" + getSubject().toString();387}388}389if (principals != null) {390ListIterator<PrincipalEntry> li = principals.listIterator();391while (li.hasNext()) {392PrincipalEntry pppe = li.next();393returnMe = returnMe + rb.getString("NEWLINE") +394pppe.getPrincipalClass() + " " +395pppe.getPrincipalName();396}397}398return returnMe;399}400}401402403