001/* Certificate.java -- base class of public-key certificates.
002   Copyright (C) 2004 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.security.cert;
040
041import java.security.InvalidKeyException;
042import java.security.NoSuchAlgorithmException;
043import java.security.NoSuchProviderException;
044import java.security.PublicKey;
045import java.security.SignatureException;
046
047import java.util.Arrays;
048import java.util.zip.Adler32;
049
050/**
051 * <p>The base class for public-key certificates.</p>
052 *
053 * <p><b>This class is deprecated in favor of the {@link
054 * java.security.cert.Certificate} class. It should not be used in new
055 * applications.</b></p>
056 */
057public abstract class Certificate
058{
059
060  // Constructors.
061  // -------------------------------------------------------------------------
062
063  public Certificate()
064  {
065    super();
066  }
067
068  // Instance methods.
069  // -------------------------------------------------------------------------
070
071  /**
072   * <p>Tests if this certificate equals another.</p>
073   *
074   * @param other The object to test.
075   * @return True if the certificates are equal.
076   */
077  public boolean equals(Object other)
078  {
079    if (other == null || !(other instanceof Certificate))
080      {
081        return false;
082      }
083    if (other == this)
084      {
085        return true;
086      }
087    try
088      {
089        return Arrays.equals(getEncoded(), ((Certificate) other).getEncoded());
090      }
091    catch (CertificateEncodingException cee)
092      {
093        return false;
094      }
095  }
096
097  /**
098   * <p>Computes a hash code for this certificate.</p>
099   *
100   * @return The hash code.
101   */
102  public int hashCode()
103  {
104    try
105      {
106        Adler32 csum = new Adler32();
107        csum.update(getEncoded());
108        return (int) csum.getValue();
109      }
110    catch (CertificateEncodingException cee)
111      {
112        return 0;
113      }
114  }
115
116  // Abstract methods.
117  // -------------------------------------------------------------------------
118
119  /**
120   * <p>Return the encoded form of this certificate.</p>
121   *
122   * @return The encoded form.
123   * @throws CertificateEncodingException If the certificate could not be
124   *   encoded.
125   */
126  public abstract byte[] getEncoded() throws CertificateEncodingException;
127
128  /**
129   * <p>Verifies the signature of this certificate.</p>
130   *
131   * @param key The signer's public key.
132   * @throws CertificateException
133   * @throws NoSuchAlgorithmException If the algorithm used to sign the
134   *   certificate is not available.
135   * @throws InvalidKeyException If the supplied key is not appropriate for the
136   *   certificate's signature algorithm.
137   * @throws NoSuchProviderException
138   * @throws SignatureException If the signature could not be verified.
139   */
140  public abstract void verify(PublicKey key)
141    throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
142           NoSuchProviderException, SignatureException;
143
144  /**
145   * <p>Verifies the signature of this certificate, using the specified security
146   * provider.</p>
147   *
148   * @param key The signer's public key.
149   * @param sigProvider The name of the signature provider.
150   * @throws CertificateException
151   * @throws NoSuchAlgorithmException If the algorithm used to sign the
152   *   certificate is not available.
153   * @throws InvalidKeyException If the supplied key is not appropriate for the
154   *   certificate's signature algorithm.
155   * @throws NoSuchProviderException If <i>sigProvider</i> is not the name of an
156   *   installed provider.
157   * @throws SignatureException If the signature could not be verified.
158   */
159  public abstract void verify(PublicKey key, String sigProvider)
160    throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
161           NoSuchProviderException, SignatureException;
162
163  /**
164   * <p>Returns a printable representation of this certificate.</p>
165   *
166   * @return The string.
167   */
168  public abstract String toString();
169
170  /**
171   * <p>Returns this certificate's public key.</p>
172   *
173   * @return The public key.
174   */
175  public abstract PublicKey getPublicKey();
176}