home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Identity.java < prev    next >
Text File  |  1997-05-20  |  12KB  |  410 lines

  1. /*
  2.  * @(#)Identity.java    1.24 97/01/27
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.security;
  24.  
  25. import java.io.Serializable;
  26. import java.util.*;
  27.  
  28. /**
  29.  * <p>This class represents identities: real-world objects such as people,
  30.  * companies or organizations whose identities can be authenticated using 
  31.  * their public keys. Identities may also be more abstract (or concrete) 
  32.  * constructs, such as daemon threads or smart cards.
  33.  *
  34.  * <p>All Identity objects have a name and a public key. Names are
  35.  * immutable. Identities may also be scoped. That is, if an Identity is
  36.  * specified to have a particular scope, then the name and public
  37.  * key of the Identity are unique within that scope.
  38.  *
  39.  * <p>An Identity also has a set of certificates (all certifying its own
  40.  * public key). The Principal names specified in these certificates need 
  41.  * not be the same, only the key.
  42.  *
  43.  * <p>An Identity can be subclassed, to include postal and email addresses,
  44.  * telephone numbers, images of faces and logos, and so on.
  45.  *
  46.  * @see IdentityScope
  47.  * @see Signer
  48.  * @see Principal
  49.  *
  50.  * @version     1.24, 01/27/97
  51.  * @author Benjamin Renaud 
  52.  */
  53. public abstract 
  54. class Identity implements Principal, Serializable {
  55.  
  56.     /**
  57.      * The name for this identity.
  58.      */
  59.     private String name;
  60.  
  61.     /**
  62.      * The public key for this identity.
  63.      */
  64.     private PublicKey publicKey;
  65.  
  66.     /**
  67.      * Generic, descriptive information about the identity.
  68.      */
  69.     String info = "No further information available.";
  70.  
  71.     /**
  72.      * The scope of the identity.
  73.      */
  74.     IdentityScope scope;
  75.  
  76.     /**
  77.      * The certificates for this identity.
  78.      */
  79.     Vector certificates;
  80.  
  81.     /**
  82.      * Constructor for serialization only.
  83.      */
  84.     protected Identity() {
  85.     this("restoring...");
  86.     }
  87.  
  88.     /**
  89.      * Constructs an identity with the specified name and scope.
  90.      *
  91.      * @param name the identity name.  
  92.      * @param scope the scope of the identity.
  93.      *
  94.      * @exception KeyManagementException if there is already an identity 
  95.      * with the same name in the scope.
  96.      */
  97.     public Identity(String name, IdentityScope scope) throws
  98.     KeyManagementException {
  99.     this(name);
  100.     this.scope = scope;
  101.     }
  102.  
  103.     /**
  104.      * Constructs an identity with the specified name and no scope.
  105.      *
  106.      * @param name the identity name.
  107.      */
  108.     public Identity(String name) {
  109.     this.name = name;
  110.     }
  111.  
  112.     /**
  113.      * Returns this identity's name.
  114.      *
  115.      * @return the name of this identity.
  116.      */
  117.     public final String getName() {
  118.     return name;
  119.     }
  120.  
  121.     /**
  122.      * Returns this identity's scope.
  123.      *
  124.      * @return the scope of this identity.
  125.      */
  126.     public final IdentityScope getScope() {
  127.     return scope;
  128.     }
  129.  
  130.     /**
  131.      * Returns this identity's public key.
  132.      * 
  133.      * @return the public key for this identity.
  134.      */
  135.     public PublicKey getPublicKey() {
  136.     return publicKey;
  137.     }
  138.  
  139.     /**
  140.      * Sets this identity's public key. The old key and all of this
  141.      * identity's certificates are removed by this operation. 
  142.      *
  143.      * @param key the public key for this identity.
  144.      *
  145.      * @exception KeyManagementException if another identity in the 
  146.      * identity's scope has the same public key, or if another exception occurs.  
  147.      */
  148.     /* Should we throw an exception if this is already set? */
  149.     public void setPublicKey(PublicKey key) throws KeyManagementException {
  150.     
  151.     check("set.public.key");
  152.     this.publicKey = key;
  153.     certificates = new Vector();
  154.     }
  155.  
  156.     /**
  157.      * Specifies a general information string for this identity.
  158.      *
  159.      * @param info the information string.
  160.      *
  161.      * @see #getInfo
  162.      */
  163.     public void setInfo(String info) {
  164.     check("set.info");
  165.     this.info = info;
  166.     }
  167.  
  168.     /**
  169.      * Returns general information previously specified for this identity.
  170.      *
  171.      * @return general information about this identity.
  172.      *
  173.      * @see #setInfo
  174.      */
  175.     public String getInfo() {
  176.     return info;
  177.     }
  178.  
  179.     /**
  180.      * Adds a certificate for this identity. If the identity has a public
  181.      * key, the public key in the certificate must be the same, and if
  182.      * the identity does not have a public key, the identity's
  183.      * public key is set to be that specified in the certificate.
  184.      *
  185.      * @param certificate the certificate to be added.
  186.      *
  187.      * @exception KeyManagementException if the certificate is not valid,
  188.      * if the public key in the certificate being added conflicts with
  189.      * this identity's public key, or if another exception occurs.
  190.      */
  191.     public void addCertificate(Certificate certificate)
  192.     throws KeyManagementException {
  193.  
  194.     check("add.certificate");
  195.  
  196.     if (certificates == null) {
  197.         certificates = new Vector();
  198.     }
  199.     if (publicKey != null) {
  200.         if (!keyEquals(publicKey, certificate.getPublicKey())) {
  201.         throw new KeyManagementException(
  202.             "public key different from cert public key");
  203.         }
  204.     } else {
  205.         publicKey = certificate.getPublicKey();
  206.     }
  207.     certificates.addElement(certificate);
  208.     }
  209.  
  210.    private boolean keyEquals(Key aKey, Key anotherKey) {
  211.     if (aKey.getFormat().equalsIgnoreCase(anotherKey.getFormat())) {
  212.         return MessageDigest.isEqual(aKey.getEncoded(), 
  213.                      anotherKey.getEncoded());
  214.     } else {
  215.         return false;
  216.     }
  217.     }
  218.  
  219.  
  220.     /**
  221.      * Removes a certificate from this identity.
  222.      *
  223.      * @param certificate the certificate to be removed.
  224.      *
  225.      * @exception KeyManagementException if the certificate is
  226.      * missing, or if another exception occurs.
  227.      */
  228.     public void removeCertificate(Certificate certificate)
  229.     throws KeyManagementException {
  230.     check("remove.certificate");
  231.     if (certificates != null) {
  232.         certificates.removeElement(certificate);
  233.     }
  234.     }
  235.  
  236.     /**
  237.      * Returns a copy of all the certificates for this identity.  
  238.      * 
  239.      * @return a copy of all the certificates for this identity.  
  240.      */
  241.     public Certificate[] certificates() {
  242.     if (certificates == null) {
  243.         return new Certificate[0];
  244.     }
  245.     int len = certificates.size();
  246.     Certificate[] certs = new Certificate[len];
  247.     certificates.copyInto(certs);
  248.     return certs;
  249.     }
  250.  
  251.     /**
  252.      * Tests for equality between the specified object and this identity.
  253.      * This first tests to see if the entities actually refer to the same
  254.      * object, in which case it returns true. Next, it checks to see if
  255.      * the entities have the same name and the same scope. If they do, 
  256.      * the method returns true. Otherwise, it calls <a href = 
  257.      * "#identityEquals">identityEquals</a>, which subclasses should 
  258.      * override.
  259.      *
  260.      * @param identity the object to test for equality with this identity.  
  261.      *
  262.      * @return true if the objects are considered equal, false otherwise.
  263.      *
  264.      * @see #identityEquals 
  265.      */
  266.     public final boolean equals(Object identity) {
  267.  
  268.     if (identity == this) {
  269.         return true;
  270.     }
  271.  
  272.     if (identity instanceof Identity) {
  273.         Identity i = (Identity)identity;
  274.         if (i.getScope() == scope && i.getName().equals(name)) {
  275.         return true;
  276.         } else {
  277.         return identityEquals(i);        
  278.         }
  279.     }
  280.     return false;
  281.     }
  282.  
  283.     /**
  284.      * Tests for equality between the specified identity and this identity.
  285.      * This method should be overriden by subclasses to test for equality. 
  286.      * The default behavior is to return true if the names and public keys 
  287.      * are equal.
  288.      *
  289.      * @param identity the identity to test for equality with this identity.
  290.      * 
  291.      * @return true if the identities are considered equal, false
  292.      * otherwise. 
  293.      *
  294.      * @see #equals 
  295.      */
  296.     protected boolean identityEquals(Identity identity) {
  297.     return (name.equals(identity.name) && 
  298.         publicKey.equals(identity.publicKey));
  299.     }
  300.  
  301.     /**
  302.      * Returns a parsable name for identity: identityName.scopeName
  303.      */
  304.     String fullName() {
  305.     String parsable = name;
  306.     if (scope != null) {
  307.         parsable += "." + scope.getName();
  308.     }
  309.     return parsable;
  310.     }
  311.  
  312.     /**
  313.      * Returns a short string describing this identity, telling its
  314.      * name and its scope (if any).
  315.      *
  316.      * @return information about this identity, such as its name and the  
  317.      * name of its scope (if any).
  318.      */
  319.     public String toString() {
  320.     check("print");
  321.     String printable = name;
  322.     if (scope != null) {
  323.         printable += "[" + scope.getName() + "]";
  324.     }
  325.     return printable;
  326.     }
  327.  
  328.     /**
  329.      * Returns a string representation of this identity, with
  330.      * optionally more details than that provided by the
  331.      * <code>toString</code> method without any arguments.
  332.      *
  333.      * @param detailed whether or not to provide detailed information.  
  334.      *
  335.      * @return information about this identity. If <code>detailed</code>
  336.      * is true, then this method returns more information than that 
  337.      * provided by the <code>toString</code> method without any arguments.
  338.      *
  339.      * @see #toString
  340.      */
  341.     public String toString(boolean detailed) {
  342.     String out = toString();
  343.     if (detailed) {
  344.         out += "\n";
  345.         out += printKeys();
  346.         out += "\n" + printCertificates();
  347.         if (info != null) {
  348.         out += "\n\t" + info;
  349.         } else {
  350.         out += "\n\tno additional information available.";
  351.         }
  352.     }      
  353.     return out;
  354.     }
  355.  
  356.     String printKeys() {
  357.     String key = "";
  358.     if (publicKey != null) {
  359.         key = "\tpublic key initialized";
  360.     } else {
  361.         key = "\tno public key";
  362.     }
  363.     return key;
  364.     }
  365.  
  366.     String printCertificates() {
  367.     String out = "";
  368.     if (certificates == null) {
  369.         return "\tno certificates";
  370.     } else {
  371.         out += "\tcertificates: \n";
  372.         Enumeration e = certificates.elements();
  373.         int i = 1;
  374.         while (e.hasMoreElements()) {
  375.         Certificate cert = (Certificate)e.nextElement();
  376.         out += "\tcertificate " + i++ +
  377.             "\tfor  : " + cert.getPrincipal() + "\n";
  378.         out += "\t\t\tfrom : " + 
  379.             cert.getGuarantor() + "\n";
  380.         }
  381.     }
  382.     return out;
  383.     }
  384.     
  385.     /**
  386.      * Returns a hashcode for this identity.
  387.      *
  388.      * @return a hashcode for this identity.
  389.      */
  390.     public int hashCode() {
  391.     String scopedName = name;
  392.     if (scope != null) {
  393.         scopedName += scope.getName();
  394.     }
  395.     return scopedName.hashCode();
  396.     }
  397.  
  398.     void check(String directive) {
  399.     staticCheck(this.getClass().getName() + "." + directive + "." +fullName());
  400.     }
  401.  
  402.     static void staticCheck(String directive) {
  403.     SecurityManager security = System.getSecurityManager();
  404.     if (security != null) {
  405.         security.checkSecurityAccess(directive);
  406.     }
  407.     }
  408. }
  409.  
  410.