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