Certificate.java revision 16641:93005a285d67
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.  Oracle designates this
81541Srgrimes * particular file as subject to the "Classpath" exception as provided
91541Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101541Srgrimes *
111541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151541Srgrimes * accompanied this code).
161541Srgrimes *
171541Srgrimes * You should have received a copy of the GNU General Public License version
181541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201541Srgrimes *
211541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221541Srgrimes * or visit www.oracle.com if you need additional information or have any
231541Srgrimes * questions.
241541Srgrimes */
251541Srgrimes
261541Srgrimes
271541Srgrimespackage javax.security.cert;
281541Srgrimes
291541Srgrimesimport java.security.PublicKey;
301541Srgrimesimport java.security.NoSuchAlgorithmException;
311541Srgrimesimport java.security.NoSuchProviderException;
321541Srgrimesimport java.security.InvalidKeyException;
331541Srgrimesimport java.security.SignatureException;
341541Srgrimes
351541Srgrimes/**
3636503Speter * <p>Abstract class for managing a variety of identity certificates.
3741026Speter * An identity certificate is a guarantee by a principal that
381541Srgrimes * a public key is that of another principal.  (A principal represents
391541Srgrimes * an entity such as an individual user, a group, or a corporation.)
401541Srgrimes *<p>
411541Srgrimes * This class is an abstraction for certificates that have different
421541Srgrimes * formats but important common uses.  For example, different types of
431541Srgrimes * certificates, such as X.509 and PGP, share general certificate
441541Srgrimes * functionality (like encoding and verifying) and
451541Srgrimes * some types of information (like a public key).
4631886Sbde * <p>
471541Srgrimes * X.509, PGP, and SDSI certificates can all be implemented by
481541Srgrimes * subclassing the Certificate class, even though they contain different
491541Srgrimes * sets of information, and they store and retrieve the information in
501541Srgrimes * different ways.
511541Srgrimes *
521541Srgrimes * <p><em>Note: The classes in the package {@code javax.security.cert}
531541Srgrimes * exist for compatibility with earlier versions of the
541541Srgrimes * Java Secure Sockets Extension (JSSE). New applications should instead
551541Srgrimes * use the standard Java SE certificate classes located in
569336Sdfr * {@code java.security.cert}.</em></p>
572997Swollman *
582997Swollman * @since 1.4
591541Srgrimes * @see X509Certificate
603305Sphk * @deprecated Use the classes in {@code java.security.cert} instead.
6112662Sdg *
6212662Sdg * @author Hemma Prafullchandra
6332011Sbde */
643305Sphk@Deprecated(since="9")
651541Srgrimespublic abstract class Certificate {
669336Sdfr
6730808Sbde    /**
681541Srgrimes     * Compares this certificate for equality with the specified
691541Srgrimes     * object. If the {@code other} object is an
701541Srgrimes     * {@code instanceof} {@code Certificate}, then
711541Srgrimes     * its encoded form is retrieved and compared with the
721541Srgrimes     * encoded form of this certificate.
731541Srgrimes     *
741541Srgrimes     * @param other the object to test for equality with this certificate.
751541Srgrimes     * @return true if the encoded forms of the two certificates
761541Srgrimes     *         match, false otherwise.
771541Srgrimes     */
781541Srgrimes    public boolean equals(Object other) {
791541Srgrimes        if (this == other)
801541Srgrimes            return true;
811541Srgrimes        if (!(other instanceof Certificate))
821541Srgrimes            return false;
831541Srgrimes        try {
841541Srgrimes            byte[] thisCert = this.getEncoded();
851541Srgrimes            byte[] otherCert = ((Certificate)other).getEncoded();
8636541Speter
8736541Speter            if (thisCert.length != otherCert.length)
889336Sdfr                return false;
891541Srgrimes            for (int i = 0; i < thisCert.length; i++)
9036541Speter                 if (thisCert[i] != otherCert[i])
911541Srgrimes                     return false;
921541Srgrimes            return true;
9336541Speter        } catch (CertificateException e) {
9412911Sphk            return false;
9512911Sphk        }
9612911Sphk    }
9712911Sphk
9812911Sphk    /**
9912911Sphk     * Returns a hashcode value for this certificate from its
10012911Sphk     * encoded form.
1019336Sdfr     *
1029336Sdfr     * @return the hashcode value.
1039759Sbde     */
1049759Sbde    public int hashCode() {
1059759Sbde        int     retval = 0;
1069759Sbde        try {
1079759Sbde            byte[] certData = this.getEncoded();
1089759Sbde            for (int i = 1; i < certData.length; i++) {
1099759Sbde                 retval += certData[i] * i;
1109759Sbde            }
1119759Sbde            return (retval);
1129759Sbde        } catch (CertificateException e) {
11338894Sbde            return (retval);
11438894Sbde        }
11538894Sbde    }
11638894Sbde
11713416Sphk    /**
11838894Sbde     * Returns the encoded form of this certificate. It is
11938894Sbde     * assumed that each certificate type would have only a single
12038894Sbde     * form of encoding; for example, X.509 certificates would
12138894Sbde     * be encoded as ASN.1 DER.
12238894Sbde     *
1239336Sdfr     * @return encoded form of this certificate
1249336Sdfr     * @exception CertificateEncodingException on internal certificate
1259336Sdfr     *            encoding failure
1269336Sdfr     */
1279336Sdfr    public abstract byte[] getEncoded() throws CertificateEncodingException;
1289336Sdfr
1299336Sdfr    /**
1309336Sdfr     * Verifies that this certificate was signed using the
1319336Sdfr     * private key that corresponds to the specified public key.
1329336Sdfr     *
1339336Sdfr     * @param key the PublicKey used to carry out the verification.
1349336Sdfr     *
1359336Sdfr     * @exception NoSuchAlgorithmException on unsupported signature
1369336Sdfr     * algorithms.
1379336Sdfr     * @exception InvalidKeyException on incorrect key.
1389336Sdfr     * @exception NoSuchProviderException if there's no default provider.
1399336Sdfr     * @exception SignatureException on signature errors.
1409336Sdfr     * @exception CertificateException on encoding errors.
1419336Sdfr     */
1429336Sdfr    public abstract void verify(PublicKey key)
1439336Sdfr        throws CertificateException, NoSuchAlgorithmException,
1449336Sdfr        InvalidKeyException, NoSuchProviderException,
1459336Sdfr        SignatureException;
1469336Sdfr
1479336Sdfr    /**
1489336Sdfr     * Verifies that this certificate was signed using the
1499336Sdfr     * private key that corresponds to the specified public key.
1509336Sdfr     * This method uses the signature verification engine
1519336Sdfr     * supplied by the specified provider.
1529336Sdfr     *
1539336Sdfr     * @param key the PublicKey used to carry out the verification.
1549336Sdfr     * @param sigProvider the name of the signature provider.
15513416Sphk     * @exception NoSuchAlgorithmException on unsupported signature algorithms.
1569336Sdfr     * @exception InvalidKeyException on incorrect key.
1579336Sdfr     * @exception NoSuchProviderException on incorrect provider.
1589336Sdfr     * @exception SignatureException on signature errors.
1599336Sdfr     * @exception CertificateException on encoding errors.
1609336Sdfr     */
1619336Sdfr    public abstract void verify(PublicKey key, String sigProvider)
1629336Sdfr        throws CertificateException, NoSuchAlgorithmException,
1639336Sdfr        InvalidKeyException, NoSuchProviderException,
1649336Sdfr        SignatureException;
1659336Sdfr
1669336Sdfr    /**
1679336Sdfr     * Returns a string representation of this certificate.
1689336Sdfr     *
1699336Sdfr     * @return a string representation of this certificate.
1709336Sdfr     */
1719336Sdfr    public abstract String toString();
1729336Sdfr
1739336Sdfr    /**
1749336Sdfr     * Gets the public key from this certificate.
1759336Sdfr     *
1769336Sdfr     * @return the public key.
1779336Sdfr     */
1789336Sdfr    public abstract PublicKey getPublicKey();
1799336Sdfr}
1809336Sdfr