home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / security / cert / Certificate.java next >
Encoding:
Java Source  |  1998-03-20  |  5.5 KB  |  162 lines

  1. /*
  2.  * @(#)Certificate.java    1.9 98/03/18
  3.  *
  4.  * Copyright 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security.cert;
  16.  
  17. import java.security.PublicKey;
  18. import java.security.NoSuchAlgorithmException;
  19. import java.security.NoSuchProviderException;
  20. import java.security.InvalidKeyException;
  21. import java.security.SignatureException;
  22.  
  23. /**
  24.  * <p>Abstract class for managing a variety of identity certificates.
  25.  * An identity certificate is a guarantee by a principal that
  26.  * a public key is that of another principal.  (A principal represents
  27.  * an entity such as an individual user, a group, or a corporation.)
  28.  *<p>
  29.  * This class is an abstraction for certificates that have different
  30.  * formats but important common uses.  For example, different types of
  31.  * certificates, such as X.509 and PGP, share general certificate
  32.  * functionality (like encoding and verifying) and 
  33.  * some types of information (like a public key).
  34.  * <p>
  35.  * X.509, PGP, and SDSI certificates can all be implemented by
  36.  * subclassing the Certificate class, even though they contain different
  37.  * sets of information, and they store and retrieve the information in
  38.  * different ways. 
  39.  * 
  40.  * @see X509Certificate
  41.  *
  42.  * @author Hemma Prafullchandra
  43.  * @version 1.9 98/03/18
  44.  */
  45. public abstract class Certificate {
  46.  
  47.     /**
  48.      * Compares this certificate for equality with the specified 
  49.      * object. If the <code>other</code> object is an 
  50.      * <code>instanceof</code> <code>Certificate</code>, then
  51.      * its encoded form is retrieved and compared with the
  52.      * encoded form of this certificate.
  53.      * 
  54.      * @param other the object to test for equality with this certificate.
  55.      * @return true iff the encoded forms of the two certificates
  56.      * match, false otherwise.
  57.      */  
  58.     public boolean equals(Object other) {
  59.         if (this == other)
  60.             return true;
  61.         if (!(other instanceof Certificate))
  62.             return false;
  63.         try {
  64.             byte[] thisCert = this.getEncoded();
  65.             byte[] otherCert = ((Certificate)other).getEncoded();
  66.  
  67.             if (thisCert.length != otherCert.length)
  68.                 return false;
  69.             for (int i = 0; i < thisCert.length; i++)
  70.                  if (thisCert[i] != otherCert[i])
  71.                      return false;
  72.             return true;
  73.         } catch (CertificateException e) {
  74.         return false;
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Returns a hashcode value for this certificate from its
  80.      * encoded form.
  81.      *
  82.      * @return the hashcode value.
  83.      */  
  84.     public int hashCode() {
  85.         int     retval = 0;
  86.         try {
  87.             byte[] certData = this.getEncoded();
  88.             for (int i = 1; i < certData.length; i++) {
  89.                  retval += certData[i] * i;
  90.             }
  91.             return(retval);
  92.         } catch (CertificateException e) {
  93.             return(retval);
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * Returns the encoded form of this certificate. It is
  99.      * assumed that each certificate type would have only a single
  100.      * form of encoding; for example, X.509 certificates would
  101.      * be encoded as ASN.1 DER.
  102.      *
  103.      * @exception CertificateEncodingException if an encoding error occurs.
  104.      */
  105.     public abstract byte[] getEncoded()
  106.         throws CertificateEncodingException;
  107.  
  108.     /**
  109.      * Verifies that this certificate was signed using the 
  110.      * private key that corresponds to the specified public key.
  111.      *
  112.      * @param key the PublicKey used to carry out the verification.
  113.      *
  114.      * @exception NoSuchAlgorithmException on unsupported signature
  115.      * algorithms.
  116.      * @exception InvalidKeyException on incorrect key.
  117.      * @exception NoSuchProviderException if there's no default provider.
  118.      * @exception SignatureException on signature errors.
  119.      * @exception CertificateException on encoding errors.
  120.      */  
  121.     public abstract void verify(PublicKey key)
  122.         throws CertificateException, NoSuchAlgorithmException,
  123.         InvalidKeyException, NoSuchProviderException,
  124.         SignatureException;
  125.  
  126.     /**
  127.  
  128.      * Verifies that this certificate was signed using the 
  129.      * private key that corresponds to the specified public key.
  130.      * This method uses the signature verification engine
  131.      * supplied by the specified provider.
  132.      *
  133.      * @param key the PublicKey used to carry out the verification.
  134.      * @param sigProvider the name of the signature provider.
  135.      *
  136.      * @exception NoSuchAlgorithmException on unsupported signature
  137.      * algorithms.
  138.      * @exception InvalidKeyException on incorrect key.
  139.      * @exception NoSuchProviderException on incorrect provider.
  140.      * @exception SignatureException on signature errors.
  141.      * @exception CertificateException on encoding errors.
  142.      */  
  143.     public abstract void verify(PublicKey key, String sigProvider)
  144.         throws CertificateException, NoSuchAlgorithmException,
  145.         InvalidKeyException, NoSuchProviderException,
  146.         SignatureException;
  147.  
  148.     /**
  149.      * Returns a string representation of this certificate.
  150.      *
  151.      * @return a string representation of this certificate.
  152.      */
  153.     public abstract String toString();
  154.  
  155.     /**
  156.      * Gets the public key from this certificate.
  157.      * 
  158.      * @return the public key.
  159.      */
  160.     public abstract PublicKey getPublicKey();
  161. }
  162.