Certificate.java revision 11581:7a4b6292286b
1/*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26
27package javax.security.cert;
28
29import java.security.PublicKey;
30import java.security.NoSuchAlgorithmException;
31import java.security.NoSuchProviderException;
32import java.security.InvalidKeyException;
33import java.security.SignatureException;
34
35/**
36 * <p>Abstract class for managing a variety of identity certificates.
37 * An identity certificate is a guarantee by a principal that
38 * a public key is that of another principal.  (A principal represents
39 * an entity such as an individual user, a group, or a corporation.)
40 *<p>
41 * This class is an abstraction for certificates that have different
42 * formats but important common uses.  For example, different types of
43 * certificates, such as X.509 and PGP, share general certificate
44 * functionality (like encoding and verifying) and
45 * some types of information (like a public key).
46 * <p>
47 * X.509, PGP, and SDSI certificates can all be implemented by
48 * subclassing the Certificate class, even though they contain different
49 * sets of information, and they store and retrieve the information in
50 * different ways.
51 *
52 * <p><em>Note: The classes in the package {@code javax.security.cert}
53 * exist for compatibility with earlier versions of the
54 * Java Secure Sockets Extension (JSSE). New applications should instead
55 * use the standard Java SE certificate classes located in
56 * {@code java.security.cert}.</em></p>
57 *
58 * @since 1.4
59 * @see X509Certificate
60 * @deprecated Use the classes in {@code java.security.cert} instead.
61 *
62 * @author Hemma Prafullchandra
63 */
64@Deprecated
65public abstract class Certificate {
66
67    /**
68     * Compares this certificate for equality with the specified
69     * object. If the {@code other} object is an
70     * {@code instanceof} {@code Certificate}, then
71     * its encoded form is retrieved and compared with the
72     * encoded form of this certificate.
73     *
74     * @param other the object to test for equality with this certificate.
75     * @return true if the encoded forms of the two certificates
76     *         match, false otherwise.
77     */
78    public boolean equals(Object other) {
79        if (this == other)
80            return true;
81        if (!(other instanceof Certificate))
82            return false;
83        try {
84            byte[] thisCert = this.getEncoded();
85            byte[] otherCert = ((Certificate)other).getEncoded();
86
87            if (thisCert.length != otherCert.length)
88                return false;
89            for (int i = 0; i < thisCert.length; i++)
90                 if (thisCert[i] != otherCert[i])
91                     return false;
92            return true;
93        } catch (CertificateException e) {
94            return false;
95        }
96    }
97
98    /**
99     * Returns a hashcode value for this certificate from its
100     * encoded form.
101     *
102     * @return the hashcode value.
103     */
104    public int hashCode() {
105        int     retval = 0;
106        try {
107            byte[] certData = this.getEncoded();
108            for (int i = 1; i < certData.length; i++) {
109                 retval += certData[i] * i;
110            }
111            return (retval);
112        } catch (CertificateException e) {
113            return (retval);
114        }
115    }
116
117    /**
118     * Returns the encoded form of this certificate. It is
119     * assumed that each certificate type would have only a single
120     * form of encoding; for example, X.509 certificates would
121     * be encoded as ASN.1 DER.
122     *
123     * @return encoded form of this certificate
124     * @exception CertificateEncodingException on internal certificate
125     *            encoding failure
126     */
127    public abstract byte[] getEncoded() throws CertificateEncodingException;
128
129    /**
130     * Verifies that this certificate was signed using the
131     * private key that corresponds to the specified public key.
132     *
133     * @param key the PublicKey used to carry out the verification.
134     *
135     * @exception NoSuchAlgorithmException on unsupported signature
136     * algorithms.
137     * @exception InvalidKeyException on incorrect key.
138     * @exception NoSuchProviderException if there's no default provider.
139     * @exception SignatureException on signature errors.
140     * @exception CertificateException on encoding errors.
141     */
142    public abstract void verify(PublicKey key)
143        throws CertificateException, NoSuchAlgorithmException,
144        InvalidKeyException, NoSuchProviderException,
145        SignatureException;
146
147    /**
148     * Verifies that this certificate was signed using the
149     * private key that corresponds to the specified public key.
150     * This method uses the signature verification engine
151     * supplied by the specified provider.
152     *
153     * @param key the PublicKey used to carry out the verification.
154     * @param sigProvider the name of the signature provider.
155     * @exception NoSuchAlgorithmException on unsupported signature algorithms.
156     * @exception InvalidKeyException on incorrect key.
157     * @exception NoSuchProviderException on incorrect provider.
158     * @exception SignatureException on signature errors.
159     * @exception CertificateException on encoding errors.
160     */
161    public abstract void verify(PublicKey key, String sigProvider)
162        throws CertificateException, NoSuchAlgorithmException,
163        InvalidKeyException, NoSuchProviderException,
164        SignatureException;
165
166    /**
167     * Returns a string representation of this certificate.
168     *
169     * @return a string representation of this certificate.
170     */
171    public abstract String toString();
172
173    /**
174     * Gets the public key from this certificate.
175     *
176     * @return the public key.
177     */
178    public abstract PublicKey getPublicKey();
179}
180