Identity.java 13.7 KB
Newer Older
1
/* Identity.java --- Identity Class
Tom Tromey committed
2
   Copyright (C) 1999, 2003, Free Software Foundation, Inc.
3

Tom Tromey committed
4
This file is part of GNU Classpath.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

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.

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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. */
37 38

package java.security;
Tom Tromey committed
39

40 41 42 43
import java.io.Serializable;
import java.util.Vector;

/**
Tom Tromey committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
 * <p>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.</p>
 *
 * <p>All Identity objects have a <i>name</i> and a <i>public key</i>. Names
 * are immutable. <i>Identities</i> may also be <b>scoped</b>. That is, if an
 * <i>Identity</i> is specified to have a particular <i>scope</i>, then the
 * <i>name</i> and <i>public key</i> of the <i>Identity</i> are unique within
 * that <i>scope</i>.</p>
 *
 * <p>An <i>Identity</i> also has a <i>set of certificates</i> (all certifying
 * its own <i>public key</i>). The <i>Principal</i> names specified in these
 * certificates need not be the same, only the key.</p>
 *
 * <p>An <i>Identity</i> can be subclassed, to include postal and email
 * addresses, telephone numbers, images of faces and logos, and so on.</p>
 *
 * @author Mark Benvenuto
 * @see IdentityScope
 * @see Signer
 * @see Principal
 * @deprecated This class is no longer used. Its functionality has been replaced
 * by <code>java.security.KeyStore</code>, the <code>java.security.cert</code>
 * package, and <code>java.security.Principal</code>.
69 70 71
 */
public abstract class Identity implements Principal, Serializable
{
72
  private static final long serialVersionUID = 3609922007826600659L;
Mark Wielaard committed
73

74 75 76 77 78 79
  private String name;
  private IdentityScope scope;
  private PublicKey publicKey;
  private String info;
  private Vector certificates;

Tom Tromey committed
80
  /** Constructor for serialization only. */
81 82 83 84 85
  protected Identity()
  {
  }

  /**
Tom Tromey committed
86 87 88 89 90 91
   * 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.
92 93 94 95 96 97 98 99 100
   */
  public Identity(String name, IdentityScope scope)
    throws KeyManagementException
  {
    this.name = name;
    this.scope = scope;
  }

  /**
Tom Tromey committed
101 102 103
   * Constructs an identity with the specified name and no scope.
   *
   * @param name the identity name.
104 105 106 107 108 109 110 111
   */
  public Identity(String name)
  {
    this.name = name;
    this.scope = null;
  }

  /**
Tom Tromey committed
112 113 114
   * Returns this identity's name.
   *
   * @return the name of this identity.
115 116 117 118 119 120 121
   */
  public final String getName()
  {
    return name;
  }

  /**
Tom Tromey committed
122 123 124
   * Returns this identity's scope.
   *
   * @return the scope of this identity.
125 126 127 128 129 130 131
   */
  public final IdentityScope getScope()
  {
    return scope;
  }

  /**
Tom Tromey committed
132 133 134 135
   * Returns this identity's public key.
   *
   * @return the public key for this identity.
   * @see #setPublicKey(java.security.PublicKey)
136 137 138 139 140 141 142
   */
  public PublicKey getPublicKey()
  {
    return publicKey;
  }

  /**
Tom Tromey committed
143 144 145 146 147 148 149 150 151 152 153
   * <p>Sets this identity's public key. The old key and all of this identity's
   * certificates are removed by this operation.</p>
   *
   * <p>First, if there is a security manager, its <code>checkSecurityAccess()
   * </code> method is called with <code>"setIdentityPublicKey"</code> as its
   * argument to see if it's ok to set the public key.</p>
   *
   * @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
154
   * <code>checkSecurityAccess()</code> method doesn't allow setting the public
Tom Tromey committed
155 156 157
   * key.
   * @see #getPublicKey()
   * @see SecurityManager#checkSecurityAccess(String)
158 159 160 161 162 163 164 165 166 167 168
   */
  public void setPublicKey(PublicKey key) throws KeyManagementException
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkSecurityAccess("setIdentityPublicKey");

    this.publicKey = key;
  }

  /**
Tom Tromey committed
169 170 171 172 173 174 175 176 177 178 179 180
   * <p>Specifies a general information string for this identity.</p>
   *
   * <p>First, if there is a security manager, its <code>checkSecurityAccess()
   * </code> method is called with <code>"setIdentityInfo"</code> as its
   * argument to see if it's ok to specify the information string.</p>
   *
   * @param info the information string.
   * @throws SecurityException if a security manager exists and its
   * <code>checkSecurityAccess()</code> method doesn't allow setting the
   * information string.
   * @see #getInfo()
   * @see SecurityManager#checkSecurityAccess(String)
181 182 183 184 185 186 187 188 189 190 191
   */
  public void setInfo(String info)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkSecurityAccess("setIdentityInfo");

    this.info = info;
  }

  /**
Tom Tromey committed
192 193 194 195
   * Returns general information previously specified for this identity.
   *
   * @return general information about this identity.
   * @see #setInfo(String)
196 197 198 199 200 201 202
   */
  public String getInfo()
  {
    return info;
  }

  /**
Tom Tromey committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
   * <p>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.</p>
   *
   * <p>First, if there is a security manager, its <code>checkSecurityAccess()
   * </code> method is called with <code>"addIdentityCertificate"</code> as its
   * argument to see if it's ok to add a certificate.</p>
   *
   * @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
   * <code>checkSecurityAccess()</code> method doesn't allow adding a
   * certificate.
   * @see SecurityManager#checkSecurityAccess(String)
220
   */
Tom Tromey committed
221
  public void addCertificate(Certificate certificate)
222 223 224 225 226 227
    throws KeyManagementException
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkSecurityAccess("addIdentityCertificate");

Tom Tromey committed
228
    // Check public key of this certificate against the first one in the vector
229 230
    if (certificates.size() > 0)
      {
Tom Tromey committed
231
	if (((Certificate) certificates.firstElement()).getPublicKey() != publicKey)
232 233 234 235 236 237
	  throw new KeyManagementException("Public key does not match");
      }
    certificates.addElement(certificate);
  }

  /**
Tom Tromey committed
238 239 240 241 242 243 244 245 246 247 248 249 250
   * <p>Removes a certificate from this identity.</p>
   *
   * <p>First, if there is a security manager, its <code>checkSecurityAccess()
   * </code> method is called with <code>"removeIdentityCertificate"</code> as
   * its argument to see if it's ok to remove a certificate.</p>
   *
   * @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
   * <code>checkSecurityAccess()</code> method doesn't allow removing a
   * certificate.
   * @see SecurityManager#checkSecurityAccess(String)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
   */
  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);
  }

  /**
Tom Tromey committed
266 267 268
   * Returns a copy of all the certificates for this identity.
   *
   * @return a copy of all the certificates for this identity.
269 270 271
   */
  public Certificate[] certificates()
  {
272
    Certificate[] certs = new Certificate[certificates.size()];
273 274 275
    int max = certificates.size();
    for (int i = 0; i < max; i++)
      certs[i] = (Certificate) certificates.elementAt(i);
Tom Tromey committed
276

277 278 279 280
    return certs;
  }

  /**
Tom Tromey committed
281 282 283 284 285 286 287 288 289 290 291
   * 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 <code>true</code>. Next, it checks to see if the
   * entities have the same <i>name</i> and the same <i>scope</i>. If they do,
   * the method returns <code>true</code>. Otherwise, it calls
   * <code>identityEquals()</code>, which subclasses should override.
   *
   * @param identity the object to test for equality with this identity.
   * @return <code>true</code> if the objects are considered equal, <code>false
   * </code>otherwise.
   * @see #identityEquals(Identity)
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
   */
  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;
  }

  /**
Tom Tromey committed
310 311 312 313 314 315 316 317 318
   * Tests for equality between the specified <code>identity</code> and this
   * <i>identity</i>. This method should be overriden by subclasses to test for
   * equality. The default behavior is to return <code>true</code> if the names
   * and public keys are equal.
   *
   * @param identity the identity to test for equality with this identity.
   * @return <code>true</code> if the identities are considered equal,
   * <code>false</code> otherwise.
   * @see #equals(Object)
319 320 321 322 323 324 325 326
   */
  protected boolean identityEquals(Identity identity)
  {
    return ((identity.getName() == this.name) &&
	    (identity.getPublicKey() == this.publicKey));
  }

  /**
Tom Tromey committed
327 328 329 330 331 332 333 334 335 336 337 338 339
   * <p>Returns a short string describing this identity, telling its name and
   * its scope (if any).</p>
   *
   * <p>First, if there is a security manager, its <code>checkSecurityAccess()
   * </code> method is called with <code>"printIdentity"</code> as its argument
   * to see if it's ok to return the string.</p>
   *
   * @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
   * <code>checkSecurityAccess()</code> method doesn't allow returning a string
   * describing this identity.
   * @see SecurityManager#checkSecurityAccess(String)
340 341 342 343 344 345 346 347 348 349 350 351
   */
  public String toString()
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkSecurityAccess("printIdentity");

    /* TODO: Insert proper format here */
    return (name + ":@" + scope + " Public Key: " + publicKey);
  }

  /**
Tom Tromey committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
   * <p>Returns a string representation of this identity, with optionally more
   * details than that provided by the <code>toString()</code> method without
   * any arguments.</p>
   *
   * <p>First, if there is a security manager, its <code>checkSecurityAccess()
   * </code> method is called with <code>"printIdentity"</code> as its argument
   * to see if it's ok to return the string.</p>
   *
   * @param detailed whether or not to provide detailed information.
   * @return information about this identity. If detailed is <code>true</code>,
   * then this method returns more information than that provided by the
   * <code>toString()</code> method without any arguments.
   * @throws SecurityException if a security manager exists and its
   * <code>checkSecurityAccess()</code> method doesn't allow returning a string
   * describing this identity.
   * @see #toString()
   * @see SecurityManager#checkSecurityAccess(String)
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
   */
  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);
      }
  }

  /**
Tom Tromey committed
389 390 391
   * Returns a hashcode for this identity.
   *
   * @return a hashcode for this identity.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
   */
  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;
  }
}