Path: blob/master/src/java.base/share/classes/java/security/IdentityScope.java
41152 views
/*1* Copyright (c) 1996, 2019, 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 java.security;2627import java.io.Serializable;28import java.util.Enumeration;29import java.util.Properties;3031/**32* <p>This class represents a scope for identities. It is an Identity33* itself, and therefore has a name and can have a scope. It can also34* optionally have a public key and associated certificates.35*36* <p>An IdentityScope can contain Identity objects of all kinds, including37* Signers. All types of Identity objects can be retrieved, added, and38* removed using the same methods. Note that it is possible, and in fact39* expected, that different types of identity scopes will40* apply different policies for their various operations on the41* various types of Identities.42*43* <p>There is a one-to-one mapping between keys and identities, and44* there can only be one copy of one key per scope. For example, suppose45* <b>Acme Software, Inc</b> is a software publisher known to a user.46* Suppose it is an Identity, that is, it has a public key, and a set of47* associated certificates. It is named in the scope using the name48* "Acme Software". No other named Identity in the scope has the same49* public key. Of course, none has the same name as well.50*51* @see Identity52* @see Signer53* @see Principal54* @see Key55*56* @author Benjamin Renaud57* @since 1.158*59* @deprecated This class is deprecated and subject to removal in a future60* version of Java SE. It has been replaced by61* {@code java.security.KeyStore}, the {@code java.security.cert} package,62* and {@code java.security.Principal}.63*/64@Deprecated(since="1.2", forRemoval=true)65@SuppressWarnings("removal")66public abstract67class IdentityScope extends Identity {6869@java.io.Serial70private static final long serialVersionUID = -2337346281189773310L;7172/* The system's scope */73private static IdentityScope scope;7475// initialize the system scope76private static void initializeSystemScope() {7778String classname = AccessController.doPrivileged(79new PrivilegedAction<>() {80public String run() {81return Security.getProperty("system.scope");82}83});8485if (classname == null) {86return;8788} else {8990try {91Class.forName(classname);92} catch (ClassNotFoundException e) {93System.err.println("unable to establish a system scope from " +94classname);95e.printStackTrace();96}97}98}99100/**101* This constructor is used for serialization only and should not102* be used by subclasses.103*/104protected IdentityScope() {105this("restoring...");106}107108/**109* Constructs a new identity scope with the specified name.110*111* @param name the scope name.112*/113public IdentityScope(String name) {114super(name);115}116117/**118* Constructs a new identity scope with the specified name and scope.119*120* @param name the scope name.121* @param scope the scope for the new identity scope.122*123* @throws KeyManagementException if there is already an identity124* with the same name in the scope.125*/126public IdentityScope(String name, IdentityScope scope)127throws KeyManagementException {128super(name, scope);129}130131/**132* Returns the system's identity scope.133*134* @return the system's identity scope, or {@code null} if none has been135* set.136*137* @see #setSystemScope138*/139public static IdentityScope getSystemScope() {140if (scope == null) {141initializeSystemScope();142}143return scope;144}145146147/**148* Sets the system's identity scope.149*150* <p>First, if there is a security manager, its151* {@code checkSecurityAccess}152* method is called with {@code "setSystemScope"}153* as its argument to see if it's ok to set the identity scope.154*155* @param scope the scope to set.156*157* @throws SecurityException if a security manager exists and its158* {@code checkSecurityAccess} method doesn't allow159* setting the identity scope.160*161* @see #getSystemScope162* @see SecurityManager#checkSecurityAccess163*/164protected static void setSystemScope(IdentityScope scope) {165check("setSystemScope");166IdentityScope.scope = scope;167}168169/**170* Returns the number of identities within this identity scope.171*172* @return the number of identities within this identity scope.173*/174public abstract int size();175176/**177* Returns the identity in this scope with the specified name (if any).178*179* @param name the name of the identity to be retrieved.180*181* @return the identity named {@code name}, or null if there are182* no identities named {@code name} in this scope.183*/184public abstract Identity getIdentity(String name);185186/**187* Retrieves the identity whose name is the same as that of the188* specified principal. (Note: Identity implements Principal.)189*190* @param principal the principal corresponding to the identity191* to be retrieved.192*193* @return the identity whose name is the same as that of the194* principal, or null if there are no identities of the same name195* in this scope.196*/197public Identity getIdentity(Principal principal) {198return getIdentity(principal.getName());199}200201/**202* Retrieves the identity with the specified public key.203*204* @param key the public key for the identity to be returned.205*206* @return the identity with the given key, or null if there are207* no identities in this scope with that key.208*/209public abstract Identity getIdentity(PublicKey key);210211/**212* Adds an identity to this identity scope.213*214* @param identity the identity to be added.215*216* @throws KeyManagementException if the identity is not217* valid, a name conflict occurs, another identity has the same218* public key as the identity being added, or another exception219* occurs. */220public abstract void addIdentity(Identity identity)221throws KeyManagementException;222223/**224* Removes an identity from this identity scope.225*226* @param identity the identity to be removed.227*228* @throws KeyManagementException if the identity is missing,229* or another exception occurs.230*/231public abstract void removeIdentity(Identity identity)232throws KeyManagementException;233234/**235* Returns an enumeration of all identities in this identity scope.236*237* @return an enumeration of all identities in this identity scope.238*/239public abstract Enumeration<Identity> identities();240241/**242* Returns a string representation of this identity scope, including243* its name, its scope name, and the number of identities in this244* identity scope.245*246* @return a string representation of this identity scope.247*/248public String toString() {249return super.toString() + "[" + size() + "]";250}251252private static void check(String directive) {253SecurityManager security = System.getSecurityManager();254if (security != null) {255security.checkSecurityAccess(directive);256}257}258259}260261262