001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.security.cert;
027
028 import java.security.PublicKey;
029 import java.security.NoSuchAlgorithmException;
030 import java.security.NoSuchProviderException;
031 import java.security.InvalidKeyException;
032 import java.security.SignatureException;
033
034 /**
035 * <p>Abstract class for managing a variety of identity certificates.
036 * An identity certificate is a guarantee by a principal that
037 * a public key is that of another principal. (A principal represents
038 * an entity such as an individual user, a group, or a corporation.)
039 *<p>
040 * This class is an abstraction for certificates that have different
041 * formats but important common uses. For example, different types of
042 * certificates, such as X.509 and PGP, share general certificate
043 * functionality (like encoding and verifying) and
044 * some types of information (like a public key).
045 * <p>
046 * X.509, PGP, and SDSI certificates can all be implemented by
047 * subclassing the Certificate class, even though they contain different
048 * sets of information, and they store and retrieve the information in
049 * different ways.
050 *
051 * <p><em>Note: The classes in the package <code>javax.security.cert</code>
052 * exist for compatibility with earlier versions of the
053 * Java Secure Sockets Extension (JSSE). New applications should instead
054 * use the standard Java SE certificate classes located in
055 * <code>java.security.cert</code>.</em></p>
056 *
057 * @since 1.4
058 * @see X509Certificate
059 *
060 * @author Hemma Prafullchandra
061 * @version 1.23
062 */
063 public abstract class Certificate {
064
065 /**
066 * Compares this certificate for equality with the specified
067 * object. If the <code>other</code> object is an
068 * <code>instanceof</code> <code>Certificate</code>, then
069 * its encoded form is retrieved and compared with the
070 * encoded form of this certificate.
071 *
072 * @param other the object to test for equality with this certificate.
073 * @return true if the encoded forms of the two certificates
074 * match, false otherwise.
075 */
076 public boolean equals(Object other) {
077 if (this == other)
078 return true;
079 if (!(other instanceof Certificate))
080 return false;
081 try {
082 byte[] this Cert = this .getEncoded();
083 byte[] otherCert = ((Certificate) other).getEncoded();
084
085 if (this Cert.length != otherCert.length)
086 return false;
087 for (int i = 0; i < this Cert.length; i++)
088 if (this Cert[i] != otherCert[i])
089 return false;
090 return true;
091 } catch (CertificateException e) {
092 return false;
093 }
094 }
095
096 /**
097 * Returns a hashcode value for this certificate from its
098 * encoded form.
099 *
100 * @return the hashcode value.
101 */
102 public int hashCode() {
103 int retval = 0;
104 try {
105 byte[] certData = this .getEncoded();
106 for (int i = 1; i < certData.length; i++) {
107 retval += certData[i] * i;
108 }
109 return (retval);
110 } catch (CertificateException e) {
111 return (retval);
112 }
113 }
114
115 /**
116 * Returns the encoded form of this certificate. It is
117 * assumed that each certificate type would have only a single
118 * form of encoding; for example, X.509 certificates would
119 * be encoded as ASN.1 DER.
120 *
121 * @return encoded form of this certificate
122 * @exception CertificateEncodingException on internal certificate
123 * encoding failure
124 */
125 public abstract byte[] getEncoded()
126 throws CertificateEncodingException;
127
128 /**
129 * Verifies that this certificate was signed using the
130 * private key that corresponds to the specified public key.
131 *
132 * @param key the PublicKey used to carry out the verification.
133 *
134 * @exception NoSuchAlgorithmException on unsupported signature
135 * algorithms.
136 * @exception InvalidKeyException on incorrect key.
137 * @exception NoSuchProviderException if there's no default provider.
138 * @exception SignatureException on signature errors.
139 * @exception CertificateException on encoding errors.
140 */
141 public abstract void verify(PublicKey key)
142 throws CertificateException, NoSuchAlgorithmException,
143 InvalidKeyException, NoSuchProviderException,
144 SignatureException;
145
146 /**
147 * Verifies that this certificate was signed using the
148 * private key that corresponds to the specified public key.
149 * This method uses the signature verification engine
150 * supplied by the specified provider.
151 *
152 * @param key the PublicKey used to carry out the verification.
153 * @param sigProvider the name of the signature provider.
154 * @exception NoSuchAlgorithmException on unsupported signature algorithms.
155 * @exception InvalidKeyException on incorrect key.
156 * @exception NoSuchProviderException on incorrect provider.
157 * @exception SignatureException on signature errors.
158 * @exception CertificateException on encoding errors.
159 */
160 public abstract void verify(PublicKey key, String sigProvider)
161 throws CertificateException, NoSuchAlgorithmException,
162 InvalidKeyException, NoSuchProviderException,
163 SignatureException;
164
165 /**
166 * Returns a string representation of this certificate.
167 *
168 * @return a string representation of this certificate.
169 */
170 public abstract String toString();
171
172 /**
173 * Gets the public key from this certificate.
174 *
175 * @return the public key.
176 */
177 public abstract PublicKey getPublicKey();
178 }
|