Path: blob/master/src/java.base/share/classes/javax/security/cert/Certificate.java
41159 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package javax.security.cert;2728import java.security.PublicKey;29import java.security.NoSuchAlgorithmException;30import java.security.NoSuchProviderException;31import java.security.InvalidKeyException;32import java.security.SignatureException;3334/**35* <p>Abstract class for managing a variety of identity certificates.36* An identity certificate is a guarantee by a principal that37* a public key is that of another principal. (A principal represents38* an entity such as an individual user, a group, or a corporation.)39*<p>40* This class is an abstraction for certificates that have different41* formats but important common uses. For example, different types of42* certificates, such as X.509 and PGP, share general certificate43* functionality (like encoding and verifying) and44* some types of information (like a public key).45* <p>46* X.509, PGP, and SDSI certificates can all be implemented by47* subclassing the Certificate class, even though they contain different48* sets of information, and they store and retrieve the information in49* different ways.50*51* <p><em>Note: The classes in the package {@code javax.security.cert}52* exist for compatibility with earlier versions of the53* Java Secure Sockets Extension (JSSE). New applications should instead54* use the standard Java SE certificate classes located in55* {@code java.security.cert}.</em></p>56*57* @since 1.458* @see X509Certificate59* @deprecated Use the classes in {@code java.security.cert} instead.60*61* @author Hemma Prafullchandra62*/63@SuppressWarnings("removal")64@Deprecated(since="9", forRemoval=true)65public abstract class Certificate {6667/**68* Constructor for subclasses to call.69*/70public Certificate() {}7172/**73* Compares this certificate for equality with the specified74* object. If the {@code other} object is an75* {@code instanceof} {@code Certificate}, then76* its encoded form is retrieved and compared with the77* encoded form of this certificate.78*79* @param other the object to test for equality with this certificate.80* @return true if the encoded forms of the two certificates81* match, false otherwise.82*/83public boolean equals(Object other) {84if (this == other)85return true;86if (!(other instanceof Certificate))87return false;88try {89byte[] thisCert = this.getEncoded();90byte[] otherCert = ((Certificate)other).getEncoded();9192if (thisCert.length != otherCert.length)93return false;94for (int i = 0; i < thisCert.length; i++)95if (thisCert[i] != otherCert[i])96return false;97return true;98} catch (CertificateException e) {99return false;100}101}102103/**104* Returns a hashcode value for this certificate from its105* encoded form.106*107* @return the hashcode value.108*/109public int hashCode() {110int retval = 0;111try {112byte[] certData = this.getEncoded();113for (int i = 1; i < certData.length; i++) {114retval += certData[i] * i;115}116return (retval);117} catch (CertificateException e) {118return (retval);119}120}121122/**123* Returns the encoded form of this certificate. It is124* assumed that each certificate type would have only a single125* form of encoding; for example, X.509 certificates would126* be encoded as ASN.1 DER.127*128* @return encoded form of this certificate129* @exception CertificateEncodingException on internal certificate130* encoding failure131*/132public abstract byte[] getEncoded() throws CertificateEncodingException;133134/**135* Verifies that this certificate was signed using the136* private key that corresponds to the specified public key.137*138* @param key the PublicKey used to carry out the verification.139*140* @exception NoSuchAlgorithmException on unsupported signature141* algorithms.142* @exception InvalidKeyException on incorrect key.143* @exception NoSuchProviderException if there's no default provider.144* @exception SignatureException on signature errors.145* @exception CertificateException on encoding errors.146*/147public abstract void verify(PublicKey key)148throws CertificateException, NoSuchAlgorithmException,149InvalidKeyException, NoSuchProviderException,150SignatureException;151152/**153* Verifies that this certificate was signed using the154* private key that corresponds to the specified public key.155* This method uses the signature verification engine156* supplied by the specified provider.157*158* @param key the PublicKey used to carry out the verification.159* @param sigProvider the name of the signature provider.160* @exception NoSuchAlgorithmException on unsupported signature algorithms.161* @exception InvalidKeyException on incorrect key.162* @exception NoSuchProviderException on incorrect provider.163* @exception SignatureException on signature errors.164* @exception CertificateException on encoding errors.165*/166public abstract void verify(PublicKey key, String sigProvider)167throws CertificateException, NoSuchAlgorithmException,168InvalidKeyException, NoSuchProviderException,169SignatureException;170171/**172* Returns a string representation of this certificate.173*174* @return a string representation of this certificate.175*/176public abstract String toString();177178/**179* Gets the public key from this certificate.180*181* @return the public key.182*/183public abstract PublicKey getPublicKey();184}185186187