1. /*
  2. * @(#)Certificate.java 1.15 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security.cert;
  8. import java.security.PublicKey;
  9. import java.security.NoSuchAlgorithmException;
  10. import java.security.NoSuchProviderException;
  11. import java.security.InvalidKeyException;
  12. import java.security.SignatureException;
  13. /**
  14. * <p>Abstract class for managing a variety of identity certificates.
  15. * An identity certificate is a binding of a principal to a public key which
  16. * is vouched for by another principal. (A principal represents
  17. * an entity such as an individual user, a group, or a corporation.)
  18. *<p>
  19. * This class is an abstraction for certificates that have different
  20. * formats but important common uses. For example, different types of
  21. * certificates, such as X.509 and PGP, share general certificate
  22. * functionality (like encoding and verifying) and
  23. * some types of information (like a public key).
  24. * <p>
  25. * X.509, PGP, and SDSI certificates can all be implemented by
  26. * subclassing the Certificate class, even though they contain different
  27. * sets of information, and they store and retrieve the information in
  28. * different ways.
  29. *
  30. * @see X509Certificate
  31. * @see CertificateFactory
  32. *
  33. * @author Hemma Prafullchandra
  34. * @version 1.15 01/11/29
  35. */
  36. public abstract class Certificate {
  37. // the certificate type
  38. private String type;
  39. /**
  40. * Creates a certificate of the specified type.
  41. *
  42. * @param type the standard name of the certificate type.
  43. * See Appendix A in the <a href=
  44. * "../../../../guide/security/CryptoSpec.html#AppA">
  45. * Java Cryptography Architecture API Specification & Reference </a>
  46. * for information about standard certificate types.
  47. */
  48. protected Certificate(String type) {
  49. this.type = type;
  50. }
  51. /**
  52. * Returns the type of this certificate.
  53. *
  54. * @return the type of this certificate.
  55. */
  56. public final String getType() {
  57. return this.type;
  58. }
  59. /**
  60. * Compares this certificate for equality with the specified
  61. * object. If the <code>other</code> object is an
  62. * <code>instanceof</code> <code>Certificate</code>, then
  63. * its encoded form is retrieved and compared with the
  64. * encoded form of this certificate.
  65. *
  66. * @param other the object to test for equality with this certificate.
  67. * @return true iff the encoded forms of the two certificates
  68. * match, false otherwise.
  69. */
  70. public boolean equals(Object other) {
  71. if (this == other)
  72. return true;
  73. if (!(other instanceof Certificate))
  74. return false;
  75. try {
  76. byte[] thisCert = this.getEncoded();
  77. byte[] otherCert = ((Certificate)other).getEncoded();
  78. if (thisCert.length != otherCert.length)
  79. return false;
  80. for (int i = 0; i < thisCert.length; i++)
  81. if (thisCert[i] != otherCert[i])
  82. return false;
  83. return true;
  84. } catch (CertificateException e) {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Returns a hashcode value for this certificate from its
  90. * encoded form.
  91. *
  92. * @return the hashcode value.
  93. */
  94. public int hashCode() {
  95. int retval = 0;
  96. try {
  97. byte[] certData = this.getEncoded();
  98. for (int i = 1; i < certData.length; i++) {
  99. retval += certData[i] * i;
  100. }
  101. return(retval);
  102. } catch (CertificateException e) {
  103. return(retval);
  104. }
  105. }
  106. /**
  107. * Returns the encoded form of this certificate. It is
  108. * assumed that each certificate type would have only a single
  109. * form of encoding; for example, X.509 certificates would
  110. * be encoded as ASN.1 DER.
  111. *
  112. * @exception CertificateEncodingException if an encoding error occurs.
  113. */
  114. public abstract byte[] getEncoded()
  115. throws CertificateEncodingException;
  116. /**
  117. * Verifies that this certificate was signed using the
  118. * private key that corresponds to the specified public key.
  119. *
  120. * @param key the PublicKey used to carry out the verification.
  121. *
  122. * @exception NoSuchAlgorithmException on unsupported signature
  123. * algorithms.
  124. * @exception InvalidKeyException on incorrect key.
  125. * @exception NoSuchProviderException if there's no default provider.
  126. * @exception SignatureException on signature errors.
  127. * @exception CertificateException on encoding errors.
  128. */
  129. public abstract void verify(PublicKey key)
  130. throws CertificateException, NoSuchAlgorithmException,
  131. InvalidKeyException, NoSuchProviderException,
  132. SignatureException;
  133. /**
  134. * Verifies that this certificate was signed using the
  135. * private key that corresponds to the specified public key.
  136. * This method uses the signature verification engine
  137. * supplied by the specified provider.
  138. *
  139. * @param key the PublicKey used to carry out the verification.
  140. * @param sigProvider the name of the signature provider.
  141. *
  142. * @exception NoSuchAlgorithmException on unsupported signature
  143. * algorithms.
  144. * @exception InvalidKeyException on incorrect key.
  145. * @exception NoSuchProviderException on incorrect provider.
  146. * @exception SignatureException on signature errors.
  147. * @exception CertificateException on encoding errors.
  148. */
  149. public abstract void verify(PublicKey key, String sigProvider)
  150. throws CertificateException, NoSuchAlgorithmException,
  151. InvalidKeyException, NoSuchProviderException,
  152. SignatureException;
  153. /**
  154. * Returns a string representation of this certificate.
  155. *
  156. * @return a string representation of this certificate.
  157. */
  158. public abstract String toString();
  159. /**
  160. * Gets the public key from this certificate.
  161. *
  162. * @return the public key.
  163. */
  164. public abstract PublicKey getPublicKey();
  165. }