1. /*
  2. * @(#)IdentityScope.java 1.49 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.io.Serializable;
  9. import java.util.Enumeration;
  10. import java.util.Properties;
  11. /**
  12. * <p>This class represents a scope for identities. It is an Identity
  13. * itself, and therefore has a name and can have a scope. It can also
  14. * optionally have a public key and associated certificates.
  15. *
  16. * <p>An IdentityScope can contain Identity objects of all kinds, including
  17. * Signers. All types of Identity objects can be retrieved, added, and
  18. * removed using the same methods. Note that it is possible, and in fact
  19. * expected, that different types of identity scopes will
  20. * apply different policies for their various operations on the
  21. * various types of Identities.
  22. *
  23. * <p>There is a one-to-one mapping between keys and identities, and
  24. * there can only be one copy of one key per scope. For example, suppose
  25. * <b>Acme Software, Inc</b> is a software publisher known to a user.
  26. * Suppose it is an Identity, that is, it has a public key, and a set of
  27. * associated certificates. It is named in the scope using the name
  28. * "Acme Software". No other named Identity in the scope has the same
  29. * public key. Of course, none has the same name as well.
  30. *
  31. * @see Identity
  32. * @see Signer
  33. * @see Principal
  34. * @see Key
  35. *
  36. * @version 1.49 03/01/23
  37. * @author Benjamin Renaud
  38. *
  39. * @deprecated This class is no longer used. Its functionality has been
  40. * replaced by <code>java.security.KeyStore</code>, the
  41. * <code>java.security.cert</code> package, and
  42. * <code>java.security.Principal</code>.
  43. */
  44. public abstract
  45. class IdentityScope extends Identity {
  46. /* The system's scope */
  47. private static IdentityScope scope;
  48. // initialize the system scope
  49. private static void initializeSystemScope() {
  50. String classname = (String) AccessController.doPrivileged(
  51. new PrivilegedAction() {
  52. public Object run() {
  53. return Security.getProperty("system.scope");
  54. }
  55. });
  56. if (classname == null) {
  57. return;
  58. } else {
  59. try {
  60. Class.forName(classname);
  61. } catch (ClassNotFoundException e) {
  62. Security.error("unable to establish a system scope from " +
  63. classname);
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. /**
  69. * This constructor is used for serialization only and should not
  70. * be used by subclasses.
  71. */
  72. protected IdentityScope() {
  73. this("restoring...");
  74. }
  75. /**
  76. * Constructs a new identity scope with the specified name.
  77. *
  78. * @param name the scope name.
  79. */
  80. public IdentityScope(String name) {
  81. super(name);
  82. }
  83. /**
  84. * Constructs a new identity scope with the specified name and scope.
  85. *
  86. * @param name the scope name.
  87. * @param scope the scope for the new identity scope.
  88. *
  89. * @exception KeyManagementException if there is already an identity
  90. * with the same name in the scope.
  91. */
  92. public IdentityScope(String name, IdentityScope scope)
  93. throws KeyManagementException {
  94. super(name, scope);
  95. }
  96. /**
  97. * Returns the system's identity scope.
  98. *
  99. * @return the system's identity scope.
  100. *
  101. * @see #setSystemScope
  102. */
  103. public static IdentityScope getSystemScope() {
  104. if (scope == null) {
  105. initializeSystemScope();
  106. }
  107. return scope;
  108. }
  109. /**
  110. * Sets the system's identity scope.
  111. *
  112. * <p>First, if there is a security manager, its
  113. * <code>checkSecurityAccess</code>
  114. * method is called with <code>"setSystemScope"</code>
  115. * as its argument to see if it's ok to set the identity scope.
  116. *
  117. * @param scope the scope to set.
  118. *
  119. * @exception SecurityException if a security manager exists and its
  120. * <code>checkSecurityAccess</code> method doesn't allow
  121. * setting the identity scope.
  122. *
  123. * @see #getSystemScope
  124. * @see SecurityManager#checkSecurityAccess
  125. */
  126. protected static void setSystemScope(IdentityScope scope) {
  127. check("setSystemScope");
  128. IdentityScope.scope = scope;
  129. }
  130. /**
  131. * Returns the number of identities within this identity scope.
  132. *
  133. * @return the number of identities within this identity scope.
  134. */
  135. public abstract int size();
  136. /**
  137. * Returns the identity in this scope with the specified name (if any).
  138. *
  139. * @param name the name of the identity to be retrieved.
  140. *
  141. * @return the identity named <code>name</code>, or null if there are
  142. * no identities named <code>name</code> in this scope.
  143. */
  144. public abstract Identity getIdentity(String name);
  145. /**
  146. * Retrieves the identity whose name is the same as that of the
  147. * specified principal. (Note: Identity implements Principal.)
  148. *
  149. * @param principal the principal corresponding to the identity
  150. * to be retrieved.
  151. *
  152. * @return the identity whose name is the same as that of the
  153. * principal, or null if there are no identities of the same name
  154. * in this scope.
  155. */
  156. public Identity getIdentity(Principal principal) {
  157. return getIdentity(principal.getName());
  158. }
  159. /**
  160. * Retrieves the identity with the specified public key.
  161. *
  162. * @param key the public key for the identity to be returned.
  163. *
  164. * @return the identity with the given key, or null if there are
  165. * no identities in this scope with that key.
  166. */
  167. public abstract Identity getIdentity(PublicKey key);
  168. /**
  169. * Adds an identity to this identity scope.
  170. *
  171. * @param identity the identity to be added.
  172. *
  173. * @exception KeyManagementException if the identity is not
  174. * valid, a name conflict occurs, another identity has the same
  175. * public key as the identity being added, or another exception
  176. * occurs. */
  177. public abstract void addIdentity(Identity identity)
  178. throws KeyManagementException;
  179. /**
  180. * Removes an identity from this identity scope.
  181. *
  182. * @param identity the identity to be removed.
  183. *
  184. * @exception KeyManagementException if the identity is missing,
  185. * or another exception occurs.
  186. */
  187. public abstract void removeIdentity(Identity identity)
  188. throws KeyManagementException;
  189. /**
  190. * Returns an enumeration of all identities in this identity scope.
  191. *
  192. * @return an enumeration of all identities in this identity scope.
  193. */
  194. public abstract Enumeration identities();
  195. /**
  196. * Returns a string representation of this identity scope, including
  197. * its name, its scope name, and the number of identities in this
  198. * identity scope.
  199. *
  200. * @return a string representation of this identity scope.
  201. */
  202. public String toString() {
  203. return super.toString() + "[" + size() + "]";
  204. }
  205. private static void check(String directive) {
  206. SecurityManager security = System.getSecurityManager();
  207. if (security != null) {
  208. security.checkSecurityAccess(directive);
  209. }
  210. }
  211. }