Certificate.java revision 15048:48509572eb3d
1/*
2 * Copyright (c) 1997, 2016, 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 *      This class is subject to removal in a future version of Java SE.
62 *
63 * @author Hemma Prafullchandra
64 */
65@Deprecated(since="9", forRemoval=true)
66public abstract class Certificate {
67
68    /**
69     * Compares this certificate for equality with the specified
70     * object. If the {@code other} object is an
71     * {@code instanceof} {@code Certificate}, then
72     * its encoded form is retrieved and compared with the
73     * encoded form of this certificate.
74     *
75     * @param other the object to test for equality with this certificate.
76     * @return true if the encoded forms of the two certificates
77     *         match, false otherwise.
78     */
79    public boolean equals(Object other) {
80        if (this == other)
81            return true;
82        if (!(other instanceof Certificate))
83            return false;
84        try {
85            byte[] thisCert = this.getEncoded();
86            byte[] otherCert = ((Certificate)other).getEncoded();
87
88            if (thisCert.length != otherCert.length)
89                return false;
90            for (int i = 0; i < thisCert.length; i++)
91                 if (thisCert[i] != otherCert[i])
92                     return false;
93            return true;
94        } catch (CertificateException e) {
95            return false;
96        }
97    }
98
99    /**
100     * Returns a hashcode value for this certificate from its
101     * encoded form.
102     *
103     * @return the hashcode value.
104     */
105    public int hashCode() {
106        int     retval = 0;
107        try {
108            byte[] certData = this.getEncoded();
109            for (int i = 1; i < certData.length; i++) {
110                 retval += certData[i] * i;
111            }
112            return (retval);
113        } catch (CertificateException e) {
114            return (retval);
115        }
116    }
117
118    /**
119     * Returns the encoded form of this certificate. It is
120     * assumed that each certificate type would have only a single
121     * form of encoding; for example, X.509 certificates would
122     * be encoded as ASN.1 DER.
123     *
124     * @return encoded form of this certificate
125     * @exception CertificateEncodingException on internal certificate
126     *            encoding failure
127     */
128    public abstract byte[] getEncoded() throws CertificateEncodingException;
129
130    /**
131     * Verifies that this certificate was signed using the
132     * private key that corresponds to the specified public key.
133     *
134     * @param key the PublicKey used to carry out the verification.
135     *
136     * @exception NoSuchAlgorithmException on unsupported signature
137     * algorithms.
138     * @exception InvalidKeyException on incorrect key.
139     * @exception NoSuchProviderException if there's no default provider.
140     * @exception SignatureException on signature errors.
141     * @exception CertificateException on encoding errors.
142     */
143    public abstract void verify(PublicKey key)
144        throws CertificateException, NoSuchAlgorithmException,
145        InvalidKeyException, NoSuchProviderException,
146        SignatureException;
147
148    /**
149     * Verifies that this certificate was signed using the
150     * private key that corresponds to the specified public key.
151     * This method uses the signature verification engine
152     * supplied by the specified provider.
153     *
154     * @param key the PublicKey used to carry out the verification.
155     * @param sigProvider the name of the signature provider.
156     * @exception NoSuchAlgorithmException on unsupported signature algorithms.
157     * @exception InvalidKeyException on incorrect key.
158     * @exception NoSuchProviderException on incorrect provider.
159     * @exception SignatureException on signature errors.
160     * @exception CertificateException on encoding errors.
161     */
162    public abstract void verify(PublicKey key, String sigProvider)
163        throws CertificateException, NoSuchAlgorithmException,
164        InvalidKeyException, NoSuchProviderException,
165        SignatureException;
166
167    /**
168     * Returns a string representation of this certificate.
169     *
170     * @return a string representation of this certificate.
171     */
172    public abstract String toString();
173
174    /**
175     * Gets the public key from this certificate.
176     *
177     * @return the public key.
178     */
179    public abstract PublicKey getPublicKey();
180}
181