1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
/* * @(#)Certificate.java 1.27 06/04/21 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.security.cert; import java.util.Arrays; import java.security.PublicKey; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.InvalidKeyException; import java.security.SignatureException; import sun.security.x509.X509CertImpl; /** * <p>Abstract class for managing a variety of identity certificates. * An identity certificate is a binding of a principal to a public key which * is vouched for by another principal. (A principal represents * an entity such as an individual user, a group, or a corporation.) *<p> * This class is an abstraction for certificates that have different * formats but important common uses. For example, different types of * certificates, such as X.509 and PGP, share general certificate * functionality (like encoding and verifying) and * some types of information (like a public key). * <p> * X.509, PGP, and SDSI certificates can all be implemented by * subclassing the Certificate class, even though they contain different * sets of information, and they store and retrieve the information in * different ways. * * @see X509Certificate * @see CertificateFactory * * @author Hemma Prafullchandra * @version 1.27, 04/21/06 */ public abstract class Certificate implements java.io.Serializable { private static final long serialVersionUID = -3585440601605666277L; // the certificate type private final String type; /** * Creates a certificate of the specified type. * * @param type the standard name of the certificate type. * See Appendix A in the <a href= * "../../../../technotes/guides/security/crypto/CryptoSpec.html#AppA"> * Java Cryptography Architecture API Specification & Reference </a> * for information about standard certificate types. */ protected Certificate(String type) { this.type = type; } /** * Returns the type of this certificate. * * @return the type of this certificate. */ public final String getType() { return this.type; } /** * Compares this certificate for equality with the specified * object. If the <code>other</code> object is an * <code>instanceof</code> <code>Certificate</code>, then * its encoded form is retrieved and compared with the * encoded form of this certificate. * * @param other the object to test for equality with this certificate. * @return true iff the encoded forms of the two certificates * match, false otherwise. */ public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Certificate)) { return false; } try { byte[] thisCert = X509CertImpl.getEncodedInternal(this); byte[] otherCert = X509CertImpl.getEncodedInternal((Certificate)other); return Arrays.equals(thisCert, otherCert); } catch (CertificateException e) { return false; } } /** * Returns a hashcode value for this certificate from its * encoded form. * * @return the hashcode value. */ public int hashCode() { int retval = 0; try { byte[] certData = X509CertImpl.getEncodedInternal(this); for (int i = 1; i < certData.length; i++) { retval += certData[i] * i; } return retval; } catch (CertificateException e) { return retval; } } /** * Returns the encoded form of this certificate. It is * assumed that each certificate type would have only a single * form of encoding; for example, X.509 certificates would * be encoded as ASN.1 DER. * * @return the encoded form of this certificate * * @exception CertificateEncodingException if an encoding error occurs. */ public abstract byte[] getEncoded() throws CertificateEncodingException; /** * Verifies that this certificate was signed using the * private key that corresponds to the specified public key. * * @param key the PublicKey used to carry out the verification. * * @exception NoSuchAlgorithmException on unsupported signature * algorithms. * @exception InvalidKeyException on incorrect key. * @exception NoSuchProviderException if there's no default provider. * @exception SignatureException on signature errors. * @exception CertificateException on encoding errors. */ public abstract void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException; /** * Verifies that this certificate was signed using the * private key that corresponds to the specified public key. * This method uses the signature verification engine * supplied by the specified provider. * * @param key the PublicKey used to carry out the verification. * @param sigProvider the name of the signature provider. * * @exception NoSuchAlgorithmException on unsupported signature * algorithms. * @exception InvalidKeyException on incorrect key. * @exception NoSuchProviderException on incorrect provider. * @exception SignatureException on signature errors. * @exception CertificateException on encoding errors. */ public abstract void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException; /** * Returns a string representation of this certificate. * * @return a string representation of this certificate. */ public abstract String toString(); /** * Gets the public key from this certificate. * * @return the public key. */ public abstract PublicKey getPublicKey(); /** * Alternate Certificate class for serialization. * @since 1.3 */ protected static class CertificateRep implements java.io.Serializable { private static final long serialVersionUID = -8563758940495660020L; private String type; private byte[] data; /** * Construct the alternate Certificate class with the Certificate * type and Certificate encoding bytes. * * <p> * * @param type the standard name of the Certificate type. <p> * * @param data the Certificate data. */ protected CertificateRep(String type, byte[] data) { this.type = type; this.data = data; } /** * Resolve the Certificate Object. * * <p> * * @return the resolved Certificate Object * * @throws java.io.ObjectStreamException if the Certificate * could not be resolved */ protected Object readResolve() throws java.io.ObjectStreamException { try { CertificateFactory cf = CertificateFactory.getInstance(type); return cf.generateCertificate (new java.io.ByteArrayInputStream(data)); } catch (CertificateException e) { throw new java.io.NotSerializableException ("java.security.cert.Certificate: " + type + ": " + e.getMessage()); } } } /** * Replace the Certificate to be serialized. * * @return the alternate Certificate object to be serialized * * @throws java.io.ObjectStreamException if a new object representing * this Certificate could not be created * @since 1.3 */ protected Object writeReplace() throws java.io.ObjectStreamException { try { return new CertificateRep(type, getEncoded()); } catch (CertificateException e) { throw new java.io.NotSerializableException ("java.security.cert.Certificate: " + type + ": " + e.getMessage()); } } }