1. /*
  2. * @(#)X509CRL.java 1.25 03/01/23
  3. *
  4. * Copyright 2003 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.NoSuchAlgorithmException;
  9. import java.security.NoSuchProviderException;
  10. import java.security.InvalidKeyException;
  11. import java.security.SignatureException;
  12. import java.security.Principal;
  13. import java.security.PublicKey;
  14. import javax.security.auth.x500.X500Principal;
  15. import java.math.BigInteger;
  16. import java.util.Date;
  17. import java.util.Set;
  18. import java.util.Arrays;
  19. import sun.security.x509.X509CRLImpl;
  20. /**
  21. * <p>
  22. * Abstract class for an X.509 Certificate Revocation List (CRL).
  23. * A CRL is a time-stamped list identifying revoked certificates.
  24. * It is signed by a Certificate Authority (CA) and made freely
  25. * available in a public repository.
  26. *
  27. * <p>Each revoked certificate is
  28. * identified in a CRL by its certificate serial number. When a
  29. * certificate-using system uses a certificate (e.g., for verifying a
  30. * remote user's digital signature), that system not only checks the
  31. * certificate signature and validity but also acquires a suitably-
  32. * recent CRL and checks that the certificate serial number is not on
  33. * that CRL. The meaning of "suitably-recent" may vary with local
  34. * policy, but it usually means the most recently-issued CRL. A CA
  35. * issues a new CRL on a regular periodic basis (e.g., hourly, daily, or
  36. * weekly). Entries are added to CRLs as revocations occur, and an
  37. * entry may be removed when the certificate expiration date is reached.
  38. * <p>
  39. * The X.509 v2 CRL format is described below in ASN.1:
  40. * <pre>
  41. * CertificateList ::= SEQUENCE {
  42. * tbsCertList TBSCertList,
  43. * signatureAlgorithm AlgorithmIdentifier,
  44. * signature BIT STRING }
  45. * </pre>
  46. * <p>
  47. * More information can be found in RFC 2459,
  48. * "Internet X.509 Public Key Infrastructure Certificate and CRL
  49. * Profile" at <A HREF="http://www.ietf.org/rfc/rfc2459.txt">
  50. * http://www.ietf.org/rfc/rfc2459.txt </A>.
  51. * <p>
  52. * The ASN.1 definition of <code>tbsCertList</code> is:
  53. * <pre>
  54. * TBSCertList ::= SEQUENCE {
  55. * version Version OPTIONAL,
  56. * -- if present, must be v2
  57. * signature AlgorithmIdentifier,
  58. * issuer Name,
  59. * thisUpdate ChoiceOfTime,
  60. * nextUpdate ChoiceOfTime OPTIONAL,
  61. * revokedCertificates SEQUENCE OF SEQUENCE {
  62. * userCertificate CertificateSerialNumber,
  63. * revocationDate ChoiceOfTime,
  64. * crlEntryExtensions Extensions OPTIONAL
  65. * -- if present, must be v2
  66. * } OPTIONAL,
  67. * crlExtensions [0] EXPLICIT Extensions OPTIONAL
  68. * -- if present, must be v2
  69. * }
  70. * </pre>
  71. * <p>
  72. * CRLs are instantiated using a certificate factory. The following is an
  73. * example of how to instantiate an X.509 CRL:
  74. * <pre><code>
  75. * InputStream inStream = new FileInputStream("fileName-of-crl");
  76. * CertificateFactory cf = CertificateFactory.getInstance("X.509");
  77. * X509CRL crl = (X509CRL)cf.generateCRL(inStream);
  78. * inStream.close();
  79. * </code></pre>
  80. *
  81. * @author Hemma Prafullchandra
  82. *
  83. * @version 1.25, 01/23/03
  84. *
  85. * @see CRL
  86. * @see CertificateFactory
  87. * @see X509Extension
  88. */
  89. public abstract class X509CRL extends CRL implements X509Extension {
  90. private transient X500Principal issuerPrincipal;
  91. /**
  92. * Constructor for X.509 CRLs.
  93. */
  94. protected X509CRL() {
  95. super("X.509");
  96. }
  97. /**
  98. * Compares this CRL for equality with the given
  99. * object. If the <code>other</code> object is an
  100. * <code>instanceof</code> <code>X509CRL</code>, then
  101. * its encoded form is retrieved and compared with the
  102. * encoded form of this CRL.
  103. *
  104. * @param other the object to test for equality with this CRL.
  105. *
  106. * @return true iff the encoded forms of the two CRLs
  107. * match, false otherwise.
  108. */
  109. public boolean equals(Object other) {
  110. if (this == other) {
  111. return true;
  112. }
  113. if (!(other instanceof X509CRL)) {
  114. return false;
  115. }
  116. try {
  117. byte[] thisCRL = X509CRLImpl.getEncodedInternal(this);
  118. byte[] otherCRL = X509CRLImpl.getEncodedInternal((X509CRL)other);
  119. return Arrays.equals(thisCRL, otherCRL);
  120. } catch (CRLException e) {
  121. return false;
  122. }
  123. }
  124. /**
  125. * Returns a hashcode value for this CRL from its
  126. * encoded form.
  127. *
  128. * @return the hashcode value.
  129. */
  130. public int hashCode() {
  131. int retval = 0;
  132. try {
  133. byte[] crlData = X509CRLImpl.getEncodedInternal(this);
  134. for (int i = 1; i < crlData.length; i++) {
  135. retval += crlData[i] * i;
  136. }
  137. return retval;
  138. } catch (CRLException e) {
  139. return retval;
  140. }
  141. }
  142. /**
  143. * Returns the ASN.1 DER-encoded form of this CRL.
  144. *
  145. * @return the encoded form of this certificate
  146. * @exception CRLException if an encoding error occurs.
  147. */
  148. public abstract byte[] getEncoded()
  149. throws CRLException;
  150. /**
  151. * Verifies that this CRL was signed using the
  152. * private key that corresponds to the given public key.
  153. *
  154. * @param key the PublicKey used to carry out the verification.
  155. *
  156. * @exception NoSuchAlgorithmException on unsupported signature
  157. * algorithms.
  158. * @exception InvalidKeyException on incorrect key.
  159. * @exception NoSuchProviderException if there's no default provider.
  160. * @exception SignatureException on signature errors.
  161. * @exception CRLException on encoding errors.
  162. */
  163. public abstract void verify(PublicKey key)
  164. throws CRLException, NoSuchAlgorithmException,
  165. InvalidKeyException, NoSuchProviderException,
  166. SignatureException;
  167. /**
  168. * Verifies that this CRL was signed using the
  169. * private key that corresponds to the given public key.
  170. * This method uses the signature verification engine
  171. * supplied by the given provider.
  172. *
  173. * @param key the PublicKey used to carry out the verification.
  174. * @param sigProvider the name of the signature provider.
  175. *
  176. * @exception NoSuchAlgorithmException on unsupported signature
  177. * algorithms.
  178. * @exception InvalidKeyException on incorrect key.
  179. * @exception NoSuchProviderException on incorrect provider.
  180. * @exception SignatureException on signature errors.
  181. * @exception CRLException on encoding errors.
  182. */
  183. public abstract void verify(PublicKey key, String sigProvider)
  184. throws CRLException, NoSuchAlgorithmException,
  185. InvalidKeyException, NoSuchProviderException,
  186. SignatureException;
  187. /**
  188. * Gets the <code>version</code> (version number) value from the CRL.
  189. * The ASN.1 definition for this is:
  190. * <pre>
  191. * version Version OPTIONAL,
  192. * -- if present, must be v2<p>
  193. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  194. * -- v3 does not apply to CRLs but appears for consistency
  195. * -- with definition of Version for certs
  196. * </pre>
  197. *
  198. * @return the version number, i.e. 1 or 2.
  199. */
  200. public abstract int getVersion();
  201. /**
  202. * Gets the <code>issuer</code> (issuer distinguished name) value from
  203. * the CRL. The issuer name identifies the entity that signed (and
  204. * issued) the CRL.
  205. *
  206. * <p>The issuer name field contains an
  207. * X.500 distinguished name (DN).
  208. * The ASN.1 definition for this is:
  209. * <pre>
  210. * issuer Name
  211. *
  212. * Name ::= CHOICE { RDNSequence }
  213. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  214. * RelativeDistinguishedName ::=
  215. * SET OF AttributeValueAssertion
  216. *
  217. * AttributeValueAssertion ::= SEQUENCE {
  218. * AttributeType,
  219. * AttributeValue }
  220. * AttributeType ::= OBJECT IDENTIFIER
  221. * AttributeValue ::= ANY
  222. * </pre>
  223. * The <code>Name</code> describes a hierarchical name composed of
  224. * attributes,
  225. * such as country name, and corresponding values, such as US.
  226. * The type of the <code>AttributeValue</code> component is determined by
  227. * the <code>AttributeType</code> in general it will be a
  228. * <code>directoryString</code>. A <code>directoryString</code> is usually
  229. * one of <code>PrintableString</code>,
  230. * <code>TeletexString</code> or <code>UniversalString</code>.
  231. *
  232. * @return a Principal whose name is the issuer distinguished name.
  233. */
  234. public abstract Principal getIssuerDN();
  235. /**
  236. * Returns the issuer (issuer distinguished name) value from the
  237. * CRL as an <code>X500Principal</code>.
  238. * <p>
  239. * It is recommended that subclasses override this method to provide
  240. * an efficient implementation.
  241. *
  242. * @return an <code>X500Principal</code> representing the issuer
  243. * distinguished name
  244. * @since 1.4
  245. */
  246. public X500Principal getIssuerX500Principal() {
  247. if (issuerPrincipal == null) {
  248. issuerPrincipal = X509CRLImpl.getIssuerX500Principal(this);
  249. }
  250. return issuerPrincipal;
  251. }
  252. /**
  253. * Gets the <code>thisUpdate</code> date from the CRL.
  254. * The ASN.1 definition for this is:
  255. * <pre>
  256. * thisUpdate ChoiceOfTime
  257. * ChoiceOfTime ::= CHOICE {
  258. * utcTime UTCTime,
  259. * generalTime GeneralizedTime }
  260. * </pre>
  261. *
  262. * @return the <code>thisUpdate</code> date from the CRL.
  263. */
  264. public abstract Date getThisUpdate();
  265. /**
  266. * Gets the <code>nextUpdate</code> date from the CRL.
  267. *
  268. * @return the <code>nextUpdate</code> date from the CRL, or null if
  269. * not present.
  270. */
  271. public abstract Date getNextUpdate();
  272. /**
  273. * Gets the CRL entry, if any, with the given certificate serialNumber.
  274. *
  275. * @param serialNumber the serial number of the certificate for which a CRL entry
  276. * is to be looked up
  277. * @return the entry with the given serial number, or null if no such entry
  278. * exists in this CRL.
  279. * @see X509CRLEntry
  280. */
  281. public abstract X509CRLEntry
  282. getRevokedCertificate(BigInteger serialNumber);
  283. /**
  284. * Gets all the entries from this CRL.
  285. * This returns a Set of X509CRLEntry objects.
  286. *
  287. * @return all the entries or null if there are none present.
  288. * @see X509CRLEntry
  289. */
  290. public abstract Set getRevokedCertificates();
  291. /**
  292. * Gets the DER-encoded CRL information, the
  293. * <code>tbsCertList</code> from this CRL.
  294. * This can be used to verify the signature independently.
  295. *
  296. * @return the DER-encoded CRL information.
  297. * @exception CRLException if an encoding error occurs.
  298. */
  299. public abstract byte[] getTBSCertList() throws CRLException;
  300. /**
  301. * Gets the <code>signature</code> value (the raw signature bits) from
  302. * the CRL.
  303. * The ASN.1 definition for this is:
  304. * <pre>
  305. * signature BIT STRING
  306. * </pre>
  307. *
  308. * @return the signature.
  309. */
  310. public abstract byte[] getSignature();
  311. /**
  312. * Gets the signature algorithm name for the CRL
  313. * signature algorithm. An example is the string "SHA-1/DSA".
  314. * The ASN.1 definition for this is:
  315. * <pre>
  316. * signatureAlgorithm AlgorithmIdentifier<p>
  317. * AlgorithmIdentifier ::= SEQUENCE {
  318. * algorithm OBJECT IDENTIFIER,
  319. * parameters ANY DEFINED BY algorithm OPTIONAL }
  320. * -- contains a value of the type
  321. * -- registered for use with the
  322. * -- algorithm object identifier value
  323. * </pre>
  324. *
  325. * <p>The algorithm name is determined from the <code>algorithm</code>
  326. * OID string.
  327. *
  328. * @return the signature algorithm name.
  329. */
  330. public abstract String getSigAlgName();
  331. /**
  332. * Gets the signature algorithm OID string from the CRL.
  333. * An OID is represented by a set of nonnegative whole numbers separated
  334. * by periods.
  335. * For example, the string "1.2.840.10040.4.3" identifies the SHA-1
  336. * with DSA signature algorithm, as per RFC 2459.
  337. *
  338. * <p>See {@link getSigAlgName()#getSigAlgName} for
  339. * relevant ASN.1 definitions.
  340. *
  341. * @return the signature algorithm OID string.
  342. */
  343. public abstract String getSigAlgOID();
  344. /**
  345. * Gets the DER-encoded signature algorithm parameters from this
  346. * CRL's signature algorithm. In most cases, the signature
  347. * algorithm parameters are null; the parameters are usually
  348. * supplied with the public key.
  349. * If access to individual parameter values is needed then use
  350. * {@link java.security.AlgorithmParameters AlgorithmParameters}
  351. * and instantiate with the name returned by
  352. * {@link getSigAlgName()#getSigAlgName}.
  353. *
  354. * <p>See {@link getSigAlgName()#getSigAlgName} for
  355. * relevant ASN.1 definitions.
  356. *
  357. * @return the DER-encoded signature algorithm parameters, or
  358. * null if no parameters are present.
  359. */
  360. public abstract byte[] getSigAlgParams();
  361. }