1. /*
  2. * @(#)X509CRL.java 1.29 03/12/19
  3. *
  4. * Copyright 2004 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.29, 12/19/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. * <strong>Denigrated</strong>, replaced by {@linkplain
  203. * #getIssuerX500Principal()}. This method returns the <code>issuer</code>
  204. * as an implementation specific Principal object, which should not be
  205. * relied upon by portable code.
  206. *
  207. * <p>
  208. * Gets the <code>issuer</code> (issuer distinguished name) value from
  209. * the CRL. The issuer name identifies the entity that signed (and
  210. * issued) the CRL.
  211. *
  212. * <p>The issuer name field contains an
  213. * X.500 distinguished name (DN).
  214. * The ASN.1 definition for this is:
  215. * <pre>
  216. * issuer Name
  217. *
  218. * Name ::= CHOICE { RDNSequence }
  219. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  220. * RelativeDistinguishedName ::=
  221. * SET OF AttributeValueAssertion
  222. *
  223. * AttributeValueAssertion ::= SEQUENCE {
  224. * AttributeType,
  225. * AttributeValue }
  226. * AttributeType ::= OBJECT IDENTIFIER
  227. * AttributeValue ::= ANY
  228. * </pre>
  229. * The <code>Name</code> describes a hierarchical name composed of
  230. * attributes,
  231. * such as country name, and corresponding values, such as US.
  232. * The type of the <code>AttributeValue</code> component is determined by
  233. * the <code>AttributeType</code> in general it will be a
  234. * <code>directoryString</code>. A <code>directoryString</code> is usually
  235. * one of <code>PrintableString</code>,
  236. * <code>TeletexString</code> or <code>UniversalString</code>.
  237. *
  238. * @return a Principal whose name is the issuer distinguished name.
  239. */
  240. public abstract Principal getIssuerDN();
  241. /**
  242. * Returns the issuer (issuer distinguished name) value from the
  243. * CRL as an <code>X500Principal</code>.
  244. * <p>
  245. * It is recommended that subclasses override this method.
  246. *
  247. * @return an <code>X500Principal</code> representing the issuer
  248. * distinguished name
  249. * @since 1.4
  250. */
  251. public X500Principal getIssuerX500Principal() {
  252. if (issuerPrincipal == null) {
  253. issuerPrincipal = X509CRLImpl.getIssuerX500Principal(this);
  254. }
  255. return issuerPrincipal;
  256. }
  257. /**
  258. * Gets the <code>thisUpdate</code> date from the CRL.
  259. * The ASN.1 definition for this is:
  260. * <pre>
  261. * thisUpdate ChoiceOfTime
  262. * ChoiceOfTime ::= CHOICE {
  263. * utcTime UTCTime,
  264. * generalTime GeneralizedTime }
  265. * </pre>
  266. *
  267. * @return the <code>thisUpdate</code> date from the CRL.
  268. */
  269. public abstract Date getThisUpdate();
  270. /**
  271. * Gets the <code>nextUpdate</code> date from the CRL.
  272. *
  273. * @return the <code>nextUpdate</code> date from the CRL, or null if
  274. * not present.
  275. */
  276. public abstract Date getNextUpdate();
  277. /**
  278. * Gets the CRL entry, if any, with the given certificate serialNumber.
  279. *
  280. * @param serialNumber the serial number of the certificate for which a CRL entry
  281. * is to be looked up
  282. * @return the entry with the given serial number, or null if no such entry
  283. * exists in this CRL.
  284. * @see X509CRLEntry
  285. */
  286. public abstract X509CRLEntry
  287. getRevokedCertificate(BigInteger serialNumber);
  288. /**
  289. * Get the CRL entry, if any, for the given certificate.
  290. *
  291. * <p>This method can be used to lookup CRL entries in indirect CRLs,
  292. * that means CRLs that contain entries from issuers other than the CRL
  293. * issuer. The default implementation will only return entries for
  294. * certificates issued by the CRL issuer. Subclasses that wish to
  295. * support indirect CRLs should override this method.
  296. *
  297. * @param certificate the certificate for which a CRL entry is to be looked
  298. * up
  299. * @return the entry for the given certificate, or null if no such entry
  300. * exists in this CRL.
  301. * @exception NullPointerException if certificate is null
  302. *
  303. * @since 1.5
  304. */
  305. public X509CRLEntry getRevokedCertificate(X509Certificate certificate) {
  306. X500Principal certIssuer = certificate.getIssuerX500Principal();
  307. X500Principal crlIssuer = getIssuerX500Principal();
  308. if (certIssuer.equals(crlIssuer) == false) {
  309. return null;
  310. }
  311. return getRevokedCertificate(certificate.getSerialNumber());
  312. }
  313. /**
  314. * Gets all the entries from this CRL.
  315. * This returns a Set of X509CRLEntry objects.
  316. *
  317. * @return all the entries or null if there are none present.
  318. * @see X509CRLEntry
  319. */
  320. public abstract Set<? extends X509CRLEntry> getRevokedCertificates();
  321. /**
  322. * Gets the DER-encoded CRL information, the
  323. * <code>tbsCertList</code> from this CRL.
  324. * This can be used to verify the signature independently.
  325. *
  326. * @return the DER-encoded CRL information.
  327. * @exception CRLException if an encoding error occurs.
  328. */
  329. public abstract byte[] getTBSCertList() throws CRLException;
  330. /**
  331. * Gets the <code>signature</code> value (the raw signature bits) from
  332. * the CRL.
  333. * The ASN.1 definition for this is:
  334. * <pre>
  335. * signature BIT STRING
  336. * </pre>
  337. *
  338. * @return the signature.
  339. */
  340. public abstract byte[] getSignature();
  341. /**
  342. * Gets the signature algorithm name for the CRL
  343. * signature algorithm. An example is the string "SHA-1/DSA".
  344. * The ASN.1 definition for this is:
  345. * <pre>
  346. * signatureAlgorithm AlgorithmIdentifier<p>
  347. * AlgorithmIdentifier ::= SEQUENCE {
  348. * algorithm OBJECT IDENTIFIER,
  349. * parameters ANY DEFINED BY algorithm OPTIONAL }
  350. * -- contains a value of the type
  351. * -- registered for use with the
  352. * -- algorithm object identifier value
  353. * </pre>
  354. *
  355. * <p>The algorithm name is determined from the <code>algorithm</code>
  356. * OID string.
  357. *
  358. * @return the signature algorithm name.
  359. */
  360. public abstract String getSigAlgName();
  361. /**
  362. * Gets the signature algorithm OID string from the CRL.
  363. * An OID is represented by a set of nonnegative whole numbers separated
  364. * by periods.
  365. * For example, the string "1.2.840.10040.4.3" identifies the SHA-1
  366. * with DSA signature algorithm, as per RFC 2459.
  367. *
  368. * <p>See {@link #getSigAlgName() getSigAlgName} for
  369. * relevant ASN.1 definitions.
  370. *
  371. * @return the signature algorithm OID string.
  372. */
  373. public abstract String getSigAlgOID();
  374. /**
  375. * Gets the DER-encoded signature algorithm parameters from this
  376. * CRL's signature algorithm. In most cases, the signature
  377. * algorithm parameters are null; the parameters are usually
  378. * supplied with the public key.
  379. * If access to individual parameter values is needed then use
  380. * {@link java.security.AlgorithmParameters AlgorithmParameters}
  381. * and instantiate with the name returned by
  382. * {@link #getSigAlgName() getSigAlgName}.
  383. *
  384. * <p>See {@link #getSigAlgName() getSigAlgName} for
  385. * relevant ASN.1 definitions.
  386. *
  387. * @return the DER-encoded signature algorithm parameters, or
  388. * null if no parameters are present.
  389. */
  390. public abstract byte[] getSigAlgParams();
  391. }