Certificate.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Certificate.java --- Certificate class
  2. Copyright (C) 1999 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security.cert;
  32. import java.security.PublicKey;
  33. import java.security.NoSuchAlgorithmException;
  34. import java.security.InvalidKeyException;
  35. import java.security.NoSuchProviderException;
  36. import java.security.SignatureException;
  37. import java.io.ObjectInputStream;
  38. import java.io.ByteArrayInputStream;
  39. import java.io.ObjectStreamException;
  40. /**
  41. The Certificate class is an abstract class used to manage
  42. identity certificates. An identity certificate is a
  43. combination of a principal and a public key which is
  44. certified by another principal. This is the puprose of
  45. Certificate Authorities (CA).
  46. This class is used to manage different types of certificates
  47. but have important common puposes. Different types of
  48. certificates like X.509 and OpenPGP share general certificate
  49. functions (like encoding and verifying) and information like
  50. public keys.
  51. X.509, OpenPGP, and SDSI can be implemented by subclassing this
  52. class even though they differ in storage methods and information
  53. stored.
  54. @since JDK 1.2
  55. @author Mark Benvenuto
  56. */
  57. public abstract class Certificate
  58. {
  59. static final long serialVersionUID = -6751606818319535583L;
  60. private String type;
  61. /**
  62. Constructs a new certificate of the specified type. An example
  63. is "X.509".
  64. @param type a valid standard name for a certificate.
  65. */
  66. protected Certificate(String type)
  67. {
  68. this.type = type;
  69. }
  70. /**
  71. Returns the Certificate type.
  72. @return a string representing the Certificate type
  73. */
  74. public final String getType()
  75. {
  76. return type;
  77. }
  78. /**
  79. Compares this Certificate to other. It checks if the
  80. object if instanceOf Certificate and then checks if
  81. the encoded form matches.
  82. @param other An Object to test for equality
  83. @return true if equal, false otherwise
  84. */
  85. public boolean equals(Object other)
  86. {
  87. if( other instanceof Certificate ) {
  88. try {
  89. Certificate x = (Certificate) other;
  90. if( getEncoded().length != x.getEncoded().length )
  91. return false;
  92. byte b1[] = getEncoded();
  93. byte b2[] = x.getEncoded();
  94. for( int i = 0; i < b1.length; i++ )
  95. if( b1[i] != b2[i] )
  96. return false;
  97. } catch( CertificateEncodingException cee ) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. Returns a hash code for this Certificate in its encoded
  106. form.
  107. @return A hash code of this class
  108. */
  109. public int hashCode()
  110. {
  111. return super.hashCode();
  112. }
  113. /**
  114. Gets the DER ASN.1 encoded format for this Certificate.
  115. It assumes each certificate has only one encoding format.
  116. Ex: X.509 is encoded as ASN.1 DER
  117. @return byte array containg encoded form
  118. @throws CertificateEncodingException if an error occurs
  119. */
  120. public abstract byte[] getEncoded() throws CertificateEncodingException;
  121. /**
  122. Verifies that this Certificate was properly signed with the
  123. PublicKey that corresponds to its private key.
  124. @param key PublicKey to verify with
  125. @throws CertificateException encoding error
  126. @throws NoSuchAlgorithmException unsupported algorithm
  127. @throws InvalidKeyException incorrect key
  128. @throws NoSuchProviderException no provider
  129. @throws SignatureException signature error
  130. */
  131. public abstract void verify(PublicKey key)
  132. throws CertificateException,
  133. NoSuchAlgorithmException,
  134. InvalidKeyException,
  135. NoSuchProviderException,
  136. SignatureException;
  137. /**
  138. Verifies that this Certificate was properly signed with the
  139. PublicKey that corresponds to its private key and uses
  140. the signature engine provided by the provider.
  141. @param key PublicKey to verify with
  142. @param sigProvider Provider to use for signature algorithm
  143. @throws CertificateException encoding error
  144. @throws NoSuchAlgorithmException unsupported algorithm
  145. @throws InvalidKeyException incorrect key
  146. @throws NoSuchProviderException incorrect provider
  147. @throws SignatureException signature error
  148. */
  149. public abstract void verify(PublicKey key,
  150. String sigProvider)
  151. throws CertificateException,
  152. NoSuchAlgorithmException,
  153. InvalidKeyException,
  154. NoSuchProviderException,
  155. SignatureException;
  156. /**
  157. Returns a string representing the Certificate.
  158. @return a string representing the Certificate.
  159. */
  160. public abstract String toString();
  161. /**
  162. Returns the public key stored in the Certificate.
  163. @return The public key
  164. */
  165. public abstract PublicKey getPublicKey();
  166. /* INNER CLASS */
  167. /**
  168. Certificate.CertificateRep is an inner class used to provide an alternate
  169. storage mechanism for serialized Certificates.
  170. */
  171. protected static class CertificateRep implements java.io.Serializable
  172. {
  173. private String type;
  174. private byte[] data;
  175. /**
  176. Create an alternate Certificate class to store a serialized Certificate
  177. @param type the name of certificate type
  178. @param data the certificate data
  179. */
  180. protected CertificateRep(String type,
  181. byte[] data)
  182. {
  183. this.type = type;
  184. this.data = data;
  185. }
  186. /**
  187. Return the stored Certificate
  188. @return the stored certificate
  189. @throws ObjectStreamException if certificate cannot be resolved
  190. */
  191. protected Object readResolve()
  192. throws ObjectStreamException
  193. {
  194. try {
  195. return new ObjectInputStream( new ByteArrayInputStream( data ) ).readObject();
  196. } catch ( Exception e ) {
  197. e.printStackTrace();
  198. throw new RuntimeException ( e.toString() );
  199. }
  200. }
  201. }
  202. }