0N/A/*
2362N/A * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * <p>This class represents identities: real-world objects such as people,
0N/A * companies or organizations whose identities can be authenticated using
0N/A * their public keys. Identities may also be more abstract (or concrete)
0N/A * constructs, such as daemon threads or smart cards.
0N/A *
0N/A * <p>All Identity objects have a name and a public key. Names are
0N/A * immutable. Identities may also be scoped. That is, if an Identity is
0N/A * specified to have a particular scope, then the name and public
0N/A * key of the Identity are unique within that scope.
0N/A *
0N/A * <p>An Identity also has a set of certificates (all certifying its own
0N/A * public key). The Principal names specified in these certificates need
0N/A * not be the same, only the key.
0N/A *
0N/A * <p>An Identity can be subclassed, to include postal and email addresses,
0N/A * telephone numbers, images of faces and logos, and so on.
0N/A *
0N/A * @see IdentityScope
0N/A * @see Signer
0N/A * @see Principal
0N/A *
0N/A * @author Benjamin Renaud
0N/A * @deprecated This class is no longer used. Its functionality has been
0N/A * replaced by <code>java.security.KeyStore</code>, the
0N/A * <code>java.security.cert</code> package, and
0N/A * <code>java.security.Principal</code>.
0N/A */
0N/A@Deprecated
0N/Apublic abstract class Identity implements Principal, Serializable {
0N/A
0N/A /** use serialVersionUID from JDK 1.1.x for interoperability */
0N/A private static final long serialVersionUID = 3609922007826600659L;
0N/A
0N/A /**
0N/A * The name for this identity.
0N/A *
0N/A * @serial
0N/A */
0N/A private String name;
0N/A
0N/A /**
0N/A * The public key for this identity.
0N/A *
0N/A * @serial
0N/A */
0N/A private PublicKey publicKey;
0N/A
0N/A /**
0N/A * Generic, descriptive information about the identity.
0N/A *
0N/A * @serial
0N/A */
0N/A String info = "No further information available.";
0N/A
0N/A /**
0N/A * The scope of the identity.
0N/A *
0N/A * @serial
0N/A */
0N/A IdentityScope scope;
0N/A
0N/A /**
0N/A * The certificates for this identity.
0N/A *
0N/A * @serial
0N/A */
0N/A Vector<Certificate> certificates;
0N/A
0N/A /**
0N/A * Constructor for serialization only.
0N/A */
0N/A protected Identity() {
0N/A this("restoring...");
0N/A }
0N/A
0N/A /**
0N/A * Constructs an identity with the specified name and scope.
0N/A *
0N/A * @param name the identity name.
0N/A * @param scope the scope of the identity.
0N/A *
0N/A * @exception KeyManagementException if there is already an identity
0N/A * with the same name in the scope.
0N/A */
0N/A public Identity(String name, IdentityScope scope) throws
0N/A KeyManagementException {
0N/A this(name);
0N/A if (scope != null) {
0N/A scope.addIdentity(this);
0N/A }
0N/A this.scope = scope;
0N/A }
0N/A
0N/A /**
0N/A * Constructs an identity with the specified name and no scope.
0N/A *
0N/A * @param name the identity name.
0N/A */
0N/A public Identity(String name) {
0N/A this.name = name;
0N/A }
0N/A
0N/A /**
0N/A * Returns this identity's name.
0N/A *
0N/A * @return the name of this identity.
0N/A */
0N/A public final String getName() {
0N/A return name;
0N/A }
0N/A
0N/A /**
0N/A * Returns this identity's scope.
0N/A *
0N/A * @return the scope of this identity.
0N/A */
0N/A public final IdentityScope getScope() {
0N/A return scope;
0N/A }
0N/A
0N/A /**
0N/A * Returns this identity's public key.
0N/A *
0N/A * @return the public key for this identity.
0N/A *
0N/A * @see #setPublicKey
0N/A */
0N/A public PublicKey getPublicKey() {
0N/A return publicKey;
0N/A }
0N/A
0N/A /**
0N/A * Sets this identity's public key. The old key and all of this
0N/A * identity's certificates are removed by this operation.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"setIdentityPublicKey"</code>
0N/A * as its argument to see if it's ok to set the public key.
0N/A *
0N/A * @param key the public key for this identity.
0N/A *
0N/A * @exception KeyManagementException if another identity in the
0N/A * identity's scope has the same public key, or if another exception occurs.
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * setting the public key.
0N/A *
0N/A * @see #getPublicKey
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A /* Should we throw an exception if this is already set? */
0N/A public void setPublicKey(PublicKey key) throws KeyManagementException {
0N/A
0N/A check("setIdentityPublicKey");
0N/A this.publicKey = key;
0N/A certificates = new Vector<Certificate>();
0N/A }
0N/A
0N/A /**
0N/A * Specifies a general information string for this identity.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"setIdentityInfo"</code>
0N/A * as its argument to see if it's ok to specify the information string.
0N/A *
0N/A * @param info the information string.
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * setting the information string.
0N/A *
0N/A * @see #getInfo
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public void setInfo(String info) {
0N/A check("setIdentityInfo");
0N/A this.info = info;
0N/A }
0N/A
0N/A /**
0N/A * Returns general information previously specified for this identity.
0N/A *
0N/A * @return general information about this identity.
0N/A *
0N/A * @see #setInfo
0N/A */
0N/A public String getInfo() {
0N/A return info;
0N/A }
0N/A
0N/A /**
0N/A * Adds a certificate for this identity. If the identity has a public
0N/A * key, the public key in the certificate must be the same, and if
0N/A * the identity does not have a public key, the identity's
0N/A * public key is set to be that specified in the certificate.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"addIdentityCertificate"</code>
0N/A * as its argument to see if it's ok to add a certificate.
0N/A *
0N/A * @param certificate the certificate to be added.
0N/A *
0N/A * @exception KeyManagementException if the certificate is not valid,
0N/A * if the public key in the certificate being added conflicts with
0N/A * this identity's public key, or if another exception occurs.
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * adding a certificate.
0N/A *
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public void addCertificate(Certificate certificate)
0N/A throws KeyManagementException {
0N/A
0N/A check("addIdentityCertificate");
0N/A
0N/A if (certificates == null) {
0N/A certificates = new Vector<Certificate>();
0N/A }
0N/A if (publicKey != null) {
0N/A if (!keyEquals(publicKey, certificate.getPublicKey())) {
0N/A throw new KeyManagementException(
0N/A "public key different from cert public key");
0N/A }
0N/A } else {
0N/A publicKey = certificate.getPublicKey();
0N/A }
0N/A certificates.addElement(certificate);
0N/A }
0N/A
0N/A private boolean keyEquals(Key aKey, Key anotherKey) {
0N/A String aKeyFormat = aKey.getFormat();
0N/A String anotherKeyFormat = anotherKey.getFormat();
0N/A if ((aKeyFormat == null) ^ (anotherKeyFormat == null))
0N/A return false;
0N/A if (aKeyFormat != null && anotherKeyFormat != null)
0N/A if (!aKeyFormat.equalsIgnoreCase(anotherKeyFormat))
0N/A return false;
0N/A return java.util.Arrays.equals(aKey.getEncoded(),
0N/A anotherKey.getEncoded());
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Removes a certificate from this identity.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"removeIdentityCertificate"</code>
0N/A * as its argument to see if it's ok to remove a certificate.
0N/A *
0N/A * @param certificate the certificate to be removed.
0N/A *
0N/A * @exception KeyManagementException if the certificate is
0N/A * missing, or if another exception occurs.
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * removing a certificate.
0N/A *
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public void removeCertificate(Certificate certificate)
0N/A throws KeyManagementException {
0N/A check("removeIdentityCertificate");
0N/A if (certificates != null) {
0N/A certificates.removeElement(certificate);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a copy of all the certificates for this identity.
0N/A *
0N/A * @return a copy of all the certificates for this identity.
0N/A */
0N/A public Certificate[] certificates() {
0N/A if (certificates == null) {
0N/A return new Certificate[0];
0N/A }
0N/A int len = certificates.size();
0N/A Certificate[] certs = new Certificate[len];
0N/A certificates.copyInto(certs);
0N/A return certs;
0N/A }
0N/A
0N/A /**
0N/A * Tests for equality between the specified object and this identity.
0N/A * This first tests to see if the entities actually refer to the same
0N/A * object, in which case it returns true. Next, it checks to see if
0N/A * the entities have the same name and the same scope. If they do,
0N/A * the method returns true. Otherwise, it calls
0N/A * {@link #identityEquals(Identity) identityEquals}, which subclasses should
0N/A * override.
0N/A *
0N/A * @param identity the object to test for equality with this identity.
0N/A *
0N/A * @return true if the objects are considered equal, false otherwise.
0N/A *
0N/A * @see #identityEquals
0N/A */
0N/A public final boolean equals(Object identity) {
0N/A
0N/A if (identity == this) {
0N/A return true;
0N/A }
0N/A
0N/A if (identity instanceof Identity) {
0N/A Identity i = (Identity)identity;
0N/A if (this.fullName().equals(i.fullName())) {
0N/A return true;
0N/A } else {
0N/A return identityEquals(i);
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Tests for equality between the specified identity and this identity.
0N/A * This method should be overriden by subclasses to test for equality.
0N/A * The default behavior is to return true if the names and public keys
0N/A * are equal.
0N/A *
0N/A * @param identity the identity to test for equality with this identity.
0N/A *
0N/A * @return true if the identities are considered equal, false
0N/A * otherwise.
0N/A *
0N/A * @see #equals
0N/A */
0N/A protected boolean identityEquals(Identity identity) {
0N/A if (!name.equalsIgnoreCase(identity.name))
0N/A return false;
0N/A
0N/A if ((publicKey == null) ^ (identity.publicKey == null))
0N/A return false;
0N/A
0N/A if (publicKey != null && identity.publicKey != null)
0N/A if (!publicKey.equals(identity.publicKey))
0N/A return false;
0N/A
0N/A return true;
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns a parsable name for identity: identityName.scopeName
0N/A */
0N/A String fullName() {
0N/A String parsable = name;
0N/A if (scope != null) {
0N/A parsable += "." + scope.getName();
0N/A }
0N/A return parsable;
0N/A }
0N/A
0N/A /**
0N/A * Returns a short string describing this identity, telling its
0N/A * name and its scope (if any).
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"printIdentity"</code>
0N/A * as its argument to see if it's ok to return the string.
0N/A *
0N/A * @return information about this identity, such as its name and the
0N/A * name of its scope (if any).
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * returning a string describing this identity.
0N/A *
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public String toString() {
0N/A check("printIdentity");
0N/A String printable = name;
0N/A if (scope != null) {
0N/A printable += "[" + scope.getName() + "]";
0N/A }
0N/A return printable;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this identity, with
0N/A * optionally more details than that provided by the
0N/A * <code>toString</code> method without any arguments.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"printIdentity"</code>
0N/A * as its argument to see if it's ok to return the string.
0N/A *
0N/A * @param detailed whether or not to provide detailed information.
0N/A *
0N/A * @return information about this identity. If <code>detailed</code>
0N/A * is true, then this method returns more information than that
0N/A * provided by the <code>toString</code> method without any arguments.
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * returning a string describing this identity.
0N/A *
0N/A * @see #toString
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public String toString(boolean detailed) {
0N/A String out = toString();
0N/A if (detailed) {
0N/A out += "\n";
0N/A out += printKeys();
0N/A out += "\n" + printCertificates();
0N/A if (info != null) {
0N/A out += "\n\t" + info;
0N/A } else {
0N/A out += "\n\tno additional information available.";
0N/A }
0N/A }
0N/A return out;
0N/A }
0N/A
0N/A String printKeys() {
0N/A String key = "";
0N/A if (publicKey != null) {
0N/A key = "\tpublic key initialized";
0N/A } else {
0N/A key = "\tno public key";
0N/A }
0N/A return key;
0N/A }
0N/A
0N/A String printCertificates() {
0N/A String out = "";
0N/A if (certificates == null) {
0N/A return "\tno certificates";
0N/A } else {
0N/A out += "\tcertificates: \n";
0N/A
0N/A int i = 1;
0N/A for (Certificate cert : certificates) {
0N/A out += "\tcertificate " + i++ +
0N/A "\tfor : " + cert.getPrincipal() + "\n";
0N/A out += "\t\t\tfrom : " +
0N/A cert.getGuarantor() + "\n";
0N/A }
0N/A }
0N/A return out;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode for this identity.
0N/A *
0N/A * @return a hashcode for this identity.
0N/A */
0N/A public int hashCode() {
0N/A return name.hashCode();
0N/A }
0N/A
0N/A private static void check(String directive) {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A security.checkSecurityAccess(directive);
0N/A }
0N/A }
0N/A}