Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/cert/Certificate.java
38918 views
/*1* Copyright (c) 1997, 2013, 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*60* @author Hemma Prafullchandra61*/62public abstract class Certificate {6364/**65* Compares this certificate for equality with the specified66* object. If the {@code other} object is an67* {@code instanceof} {@code Certificate}, then68* its encoded form is retrieved and compared with the69* encoded form of this certificate.70*71* @param other the object to test for equality with this certificate.72* @return true if the encoded forms of the two certificates73* match, false otherwise.74*/75public boolean equals(Object other) {76if (this == other)77return true;78if (!(other instanceof Certificate))79return false;80try {81byte[] thisCert = this.getEncoded();82byte[] otherCert = ((Certificate)other).getEncoded();8384if (thisCert.length != otherCert.length)85return false;86for (int i = 0; i < thisCert.length; i++)87if (thisCert[i] != otherCert[i])88return false;89return true;90} catch (CertificateException e) {91return false;92}93}9495/**96* Returns a hashcode value for this certificate from its97* encoded form.98*99* @return the hashcode value.100*/101public int hashCode() {102int retval = 0;103try {104byte[] certData = this.getEncoded();105for (int i = 1; i < certData.length; i++) {106retval += certData[i] * i;107}108return (retval);109} catch (CertificateException e) {110return (retval);111}112}113114/**115* Returns the encoded form of this certificate. It is116* assumed that each certificate type would have only a single117* form of encoding; for example, X.509 certificates would118* be encoded as ASN.1 DER.119*120* @return encoded form of this certificate121* @exception CertificateEncodingException on internal certificate122* encoding failure123*/124public abstract byte[] getEncoded() throws CertificateEncodingException;125126/**127* Verifies that this certificate was signed using the128* private key that corresponds to the specified public key.129*130* @param key the PublicKey used to carry out the verification.131*132* @exception NoSuchAlgorithmException on unsupported signature133* algorithms.134* @exception InvalidKeyException on incorrect key.135* @exception NoSuchProviderException if there's no default provider.136* @exception SignatureException on signature errors.137* @exception CertificateException on encoding errors.138*/139public abstract void verify(PublicKey key)140throws CertificateException, NoSuchAlgorithmException,141InvalidKeyException, NoSuchProviderException,142SignatureException;143144/**145* Verifies that this certificate was signed using the146* private key that corresponds to the specified public key.147* This method uses the signature verification engine148* supplied by the specified provider.149*150* @param key the PublicKey used to carry out the verification.151* @param sigProvider the name of the signature provider.152* @exception NoSuchAlgorithmException on unsupported signature algorithms.153* @exception InvalidKeyException on incorrect key.154* @exception NoSuchProviderException on incorrect provider.155* @exception SignatureException on signature errors.156* @exception CertificateException on encoding errors.157*/158public abstract void verify(PublicKey key, String sigProvider)159throws CertificateException, NoSuchAlgorithmException,160InvalidKeyException, NoSuchProviderException,161SignatureException;162163/**164* Returns a string representation of this certificate.165*166* @return a string representation of this certificate.167*/168public abstract String toString();169170/**171* Gets the public key from this certificate.172*173* @return the public key.174*/175public abstract PublicKey getPublicKey();176}177178179