/* Identity.java --- Identity Class Copyright (C) 1999, 2003, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.security; import java.io.Serializable; import java.util.Vector; /** *

This class represents identities: real-world objects such as people, * companies or organizations whose identities can be authenticated using their * public keys. Identities may also be more abstract (or concrete) constructs, * such as daemon threads or smart cards.

* *

All Identity objects have a name and a public key. Names * are immutable. Identities may also be scoped. That is, if an * Identity is specified to have a particular scope, then the * name and public key of the Identity are unique within * that scope.

* *

An Identity also has a set of certificates (all certifying * its own public key). The Principal names specified in these * certificates need not be the same, only the key.

* *

An Identity can be subclassed, to include postal and email * addresses, telephone numbers, images of faces and logos, and so on.

* * @author Mark Benvenuto * @see IdentityScope * @see Signer * @see Principal * @deprecated This class is no longer used. Its functionality has been replaced * by java.security.KeyStore, the java.security.cert * package, and java.security.Principal. */ public abstract class Identity implements Principal, Serializable { private static final long serialVersionUID = 3609922007826600659L; private String name; private IdentityScope scope; private PublicKey publicKey; private String info; private Vector certificates; /** Constructor for serialization only. */ protected Identity() { } /** * Constructs an identity with the specified name and scope. * * @param name the identity name. * @param scope the scope of the identity. * @throws KeyManagementException if there is already an identity with the * same name in the scope. */ public Identity(String name, IdentityScope scope) throws KeyManagementException { this.name = name; this.scope = scope; } /** * Constructs an identity with the specified name and no scope. * * @param name the identity name. */ public Identity(String name) { this.name = name; this.scope = null; } /** * Returns this identity's name. * * @return the name of this identity. */ public final String getName() { return name; } /** * Returns this identity's scope. * * @return the scope of this identity. */ public final IdentityScope getScope() { return scope; } /** * Returns this identity's public key. * * @return the public key for this identity. * @see #setPublicKey(java.security.PublicKey) */ public PublicKey getPublicKey() { return publicKey; } /** *

Sets this identity's public key. The old key and all of this identity's * certificates are removed by this operation.

* *

First, if there is a security manager, its checkSecurityAccess() * method is called with "setIdentityPublicKey" as its * argument to see if it's ok to set the public key.

* * @param key the public key for this identity. * @throws KeyManagementException if another identity in the identity's scope * has the same public key, or if another exception occurs. * @throws SecurityException if a security manager exists and its * checkSecurityAccess() method doesn't allow setting the public * key. * @see #getPublicKey() * @see SecurityManager#checkSecurityAccess(String) */ public void setPublicKey(PublicKey key) throws KeyManagementException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSecurityAccess("setIdentityPublicKey"); this.publicKey = key; } /** *

Specifies a general information string for this identity.

* *

First, if there is a security manager, its checkSecurityAccess() * method is called with "setIdentityInfo" as its * argument to see if it's ok to specify the information string.

* * @param info the information string. * @throws SecurityException if a security manager exists and its * checkSecurityAccess() method doesn't allow setting the * information string. * @see #getInfo() * @see SecurityManager#checkSecurityAccess(String) */ public void setInfo(String info) { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSecurityAccess("setIdentityInfo"); this.info = info; } /** * Returns general information previously specified for this identity. * * @return general information about this identity. * @see #setInfo(String) */ public String getInfo() { return info; } /** *

Adds a certificate for this identity. If the identity has a public key, * the public key in the certificate must be the same, and if the identity * does not have a public key, the identity's public key is set to be that * specified in the certificate.

* *

First, if there is a security manager, its checkSecurityAccess() * method is called with "addIdentityCertificate" as its * argument to see if it's ok to add a certificate.

* * @param certificate the certificate to be added. * @throws KeyManagementException if the certificate is not valid, if the * public key in the certificate being added conflicts with this identity's * public key, or if another exception occurs. * @throws SecurityException if a security manager exists and its * checkSecurityAccess() method doesn't allow adding a * certificate. * @see SecurityManager#checkSecurityAccess(String) */ public void addCertificate(Certificate certificate) throws KeyManagementException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSecurityAccess("addIdentityCertificate"); // Check public key of this certificate against the first one in the vector if (certificates.size() > 0) { if (((Certificate) certificates.firstElement()).getPublicKey() != publicKey) throw new KeyManagementException("Public key does not match"); } certificates.addElement(certificate); } /** *

Removes a certificate from this identity.

* *

First, if there is a security manager, its checkSecurityAccess() * method is called with "removeIdentityCertificate" as * its argument to see if it's ok to remove a certificate.

* * @param certificate the certificate to be removed. * @throws KeyManagementException if the certificate is missing, or if * another exception occurs. * @throws SecurityException if a security manager exists and its * checkSecurityAccess() method doesn't allow removing a * certificate. * @see SecurityManager#checkSecurityAccess(String) */ public void removeCertificate(Certificate certificate) throws KeyManagementException { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSecurityAccess("removeIdentityCertificate"); if (certificates.contains(certificate) == false) throw new KeyManagementException("Certificate not found"); certificates.removeElement(certificate); } /** * Returns a copy of all the certificates for this identity. * * @return a copy of all the certificates for this identity. */ public Certificate[] certificates() { Certificate[] certs = new Certificate[certificates.size()]; int max = certificates.size(); for (int i = 0; i < max; i++) certs[i] = (Certificate) certificates.elementAt(i); return certs; } /** * Tests for equality between the specified object and this identity. This * first tests to see if the entities actually refer to the same object, in * which case it returns true. Next, it checks to see if the * entities have the same name and the same scope. If they do, * the method returns true. Otherwise, it calls * identityEquals(), which subclasses should override. * * @param identity the object to test for equality with this identity. * @return true if the objects are considered equal, false * otherwise. * @see #identityEquals(Identity) */ public final boolean equals(Object identity) { if (identity instanceof Identity) { if (identity == this) return true; if ((((Identity) identity).getName() == this.name) && (((Identity) identity).getScope() == this.scope)) return true; return identityEquals((Identity) identity); } return false; } /** * Tests for equality between the specified identity and this * identity. This method should be overriden by subclasses to test for * equality. The default behavior is to return true if the names * and public keys are equal. * * @param identity the identity to test for equality with this identity. * @return true if the identities are considered equal, * false otherwise. * @see #equals(Object) */ protected boolean identityEquals(Identity identity) { return ((identity.getName() == this.name) && (identity.getPublicKey() == this.publicKey)); } /** *

Returns a short string describing this identity, telling its name and * its scope (if any).

* *

First, if there is a security manager, its checkSecurityAccess() * method is called with "printIdentity" as its argument * to see if it's ok to return the string.

* * @return information about this identity, such as its name and the name of * its scope (if any). * @throws SecurityException if a security manager exists and its * checkSecurityAccess() method doesn't allow returning a string * describing this identity. * @see SecurityManager#checkSecurityAccess(String) */ public String toString() { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSecurityAccess("printIdentity"); /* TODO: Insert proper format here */ return (name + ":@" + scope + " Public Key: " + publicKey); } /** *

Returns a string representation of this identity, with optionally more * details than that provided by the toString() method without * any arguments.

* *

First, if there is a security manager, its checkSecurityAccess() * method is called with "printIdentity" as its argument * to see if it's ok to return the string.

* * @param detailed whether or not to provide detailed information. * @return information about this identity. If detailed is true, * then this method returns more information than that provided by the * toString() method without any arguments. * @throws SecurityException if a security manager exists and its * checkSecurityAccess() method doesn't allow returning a string * describing this identity. * @see #toString() * @see SecurityManager#checkSecurityAccess(String) */ public String toString(boolean detailed) { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSecurityAccess("printIdentity"); if (detailed) { /* TODO: Insert proper detailed format here */ return (name + ":@" + scope + " Public Key: " + publicKey); } else { /* TODO: Insert proper format here */ return (name + ":@" + scope + " Public Key: " + publicKey); } } /** * Returns a hashcode for this identity. * * @return a hashcode for this identity. */ public int hashCode() { int ret = name.hashCode(); if (publicKey != null) ret |= publicKey.hashCode(); if (scope != null) ret |= scope.hashCode(); if (info != null) ret |= info.hashCode(); if (certificates != null) ret |= certificates.hashCode(); return ret; } }