1/* 2 * Copyright (c) 1997, 2017, 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 26package java.security.cert; 27 28import java.util.Arrays; 29 30import java.security.Provider; 31import java.security.PublicKey; 32import java.security.NoSuchAlgorithmException; 33import java.security.NoSuchProviderException; 34import java.security.InvalidKeyException; 35import java.security.SignatureException; 36 37import sun.security.x509.X509CertImpl; 38 39/** 40 * <p>Abstract class for managing a variety of identity certificates. 41 * An identity certificate is a binding of a principal to a public key which 42 * is vouched for by another principal. (A principal represents 43 * an entity such as an individual user, a group, or a corporation.) 44 * <p> 45 * This class is an abstraction for certificates that have different 46 * formats but important common uses. For example, different types of 47 * certificates, such as X.509 and PGP, share general certificate 48 * functionality (like encoding and verifying) and 49 * some types of information (like a public key). 50 * <p> 51 * X.509, PGP, and SDSI certificates can all be implemented by 52 * subclassing the Certificate class, even though they contain different 53 * sets of information, and they store and retrieve the information in 54 * different ways. 55 * 56 * @see X509Certificate 57 * @see CertificateFactory 58 * 59 * @author Hemma Prafullchandra 60 * @since 1.2 61 */ 62 63public abstract class Certificate implements java.io.Serializable { 64 65 private static final long serialVersionUID = -3585440601605666277L; 66 67 // the certificate type 68 private final String type; 69 70 /** Cache the hash code for the certiticate */ 71 private int hash = -1; // Default to -1 72 73 /** 74 * Creates a certificate of the specified type. 75 * 76 * @param type the standard name of the certificate type. 77 * See the CertificateFactory section in the <a href= 78 * "{@docRoot}/../specs/security/standard-names.html#certificatefactory-types"> 79 * Java Security Standard Algorithm Names Specification</a> 80 * for information about standard certificate types. 81 */ 82 protected Certificate(String type) { 83 this.type = type; 84 } 85 86 /** 87 * Returns the type of this certificate. 88 * 89 * @return the type of this certificate. 90 */ 91 public final String getType() { 92 return this.type; 93 } 94 95 /** 96 * Compares this certificate for equality with the specified 97 * object. If the {@code other} object is an 98 * {@code instanceof} {@code Certificate}, then 99 * its encoded form is retrieved and compared with the 100 * encoded form of this certificate. 101 * 102 * @param other the object to test for equality with this certificate. 103 * @return true iff the encoded forms of the two certificates 104 * match, false otherwise. 105 */ 106 public boolean equals(Object other) { 107 if (this == other) { 108 return true; 109 } 110 if (!(other instanceof Certificate)) { 111 return false; 112 } 113 try { 114 byte[] thisCert = X509CertImpl.getEncodedInternal(this); 115 byte[] otherCert = X509CertImpl.getEncodedInternal((Certificate)other); 116 117 return Arrays.equals(thisCert, otherCert); 118 } catch (CertificateException e) { 119 return false; 120 } 121 } 122 123 /** 124 * Returns a hashcode value for this certificate from its 125 * encoded form. 126 * 127 * @return the hashcode value. 128 */ 129 public int hashCode() { 130 int h = hash; 131 if (h == -1) { 132 try { 133 h = Arrays.hashCode(X509CertImpl.getEncodedInternal(this)); 134 } catch (CertificateException e) { 135 h = 0; 136 } 137 hash = h; 138 } 139 return h; 140 } 141 142 /** 143 * Returns the encoded form of this certificate. It is 144 * assumed that each certificate type would have only a single 145 * form of encoding; for example, X.509 certificates would 146 * be encoded as ASN.1 DER. 147 * 148 * @return the encoded form of this certificate 149 * 150 * @exception CertificateEncodingException if an encoding error occurs. 151 */ 152 public abstract byte[] getEncoded() 153 throws CertificateEncodingException; 154 155 /** 156 * Verifies that this certificate was signed using the 157 * private key that corresponds to the specified public key. 158 * 159 * @param key the PublicKey used to carry out the verification. 160 * 161 * @exception NoSuchAlgorithmException on unsupported signature 162 * algorithms. 163 * @exception InvalidKeyException on incorrect key. 164 * @exception NoSuchProviderException if there's no default provider. 165 * @exception SignatureException on signature errors. 166 * @exception CertificateException on encoding errors. 167 */ 168 public abstract void verify(PublicKey key) 169 throws CertificateException, NoSuchAlgorithmException, 170 InvalidKeyException, NoSuchProviderException, 171 SignatureException; 172 173 /** 174 * Verifies that this certificate was signed using the 175 * private key that corresponds to the specified public key. 176 * This method uses the signature verification engine 177 * supplied by the specified provider. 178 * 179 * @param key the PublicKey used to carry out the verification. 180 * @param sigProvider the name of the signature provider. 181 * 182 * @exception NoSuchAlgorithmException on unsupported signature 183 * algorithms. 184 * @exception InvalidKeyException on incorrect key. 185 * @exception NoSuchProviderException on incorrect provider. 186 * @exception SignatureException on signature errors. 187 * @exception CertificateException on encoding errors. 188 */ 189 public abstract void verify(PublicKey key, String sigProvider) 190 throws CertificateException, NoSuchAlgorithmException, 191 InvalidKeyException, NoSuchProviderException, 192 SignatureException; 193 194 /** 195 * Verifies that this certificate was signed using the 196 * private key that corresponds to the specified public key. 197 * This method uses the signature verification engine 198 * supplied by the specified provider. Note that the specified 199 * Provider object does not have to be registered in the provider list. 200 * 201 * <p> This method was added to version 1.8 of the Java Platform 202 * Standard Edition. In order to maintain backwards compatibility with 203 * existing service providers, this method cannot be {@code abstract} 204 * and by default throws an {@code UnsupportedOperationException}. 205 * 206 * @param key the PublicKey used to carry out the verification. 207 * @param sigProvider the signature provider. 208 * 209 * @exception NoSuchAlgorithmException on unsupported signature 210 * algorithms. 211 * @exception InvalidKeyException on incorrect key. 212 * @exception SignatureException on signature errors. 213 * @exception CertificateException on encoding errors. 214 * @exception UnsupportedOperationException if the method is not supported 215 * @since 1.8 216 */ 217 public void verify(PublicKey key, Provider sigProvider) 218 throws CertificateException, NoSuchAlgorithmException, 219 InvalidKeyException, SignatureException { 220 throw new UnsupportedOperationException(); 221 } 222 223 /** 224 * Returns a string representation of this certificate. 225 * 226 * @return a string representation of this certificate. 227 */ 228 public abstract String toString(); 229 230 /** 231 * Gets the public key from this certificate. 232 * 233 * @return the public key. 234 */ 235 public abstract PublicKey getPublicKey(); 236 237 /** 238 * Alternate Certificate class for serialization. 239 * @since 1.3 240 */ 241 protected static class CertificateRep implements java.io.Serializable { 242 243 private static final long serialVersionUID = -8563758940495660020L; 244 245 private String type; 246 private byte[] data; 247 248 /** 249 * Construct the alternate Certificate class with the Certificate 250 * type and Certificate encoding bytes. 251 * 252 * @param type the standard name of the Certificate type. 253 * 254 * @param data the Certificate data. 255 */ 256 protected CertificateRep(String type, byte[] data) { 257 this.type = type; 258 this.data = data; 259 } 260 261 /** 262 * Resolve the Certificate Object. 263 * 264 * @return the resolved Certificate Object 265 * 266 * @throws java.io.ObjectStreamException if the Certificate 267 * could not be resolved 268 */ 269 protected Object readResolve() throws java.io.ObjectStreamException { 270 try { 271 CertificateFactory cf = CertificateFactory.getInstance(type); 272 return cf.generateCertificate 273 (new java.io.ByteArrayInputStream(data)); 274 } catch (CertificateException e) { 275 throw new java.io.NotSerializableException 276 ("java.security.cert.Certificate: " + 277 type + 278 ": " + 279 e.getMessage()); 280 } 281 } 282 } 283 284 /** 285 * Replace the Certificate to be serialized. 286 * 287 * @return the alternate Certificate object to be serialized 288 * 289 * @throws java.io.ObjectStreamException if a new object representing 290 * this Certificate could not be created 291 * @since 1.3 292 */ 293 protected Object writeReplace() throws java.io.ObjectStreamException { 294 try { 295 return new CertificateRep(type, getEncoded()); 296 } catch (CertificateException e) { 297 throw new java.io.NotSerializableException 298 ("java.security.cert.Certificate: " + 299 type + 300 ": " + 301 e.getMessage()); 302 } 303 } 304} 305