1. /*
  2. * @(#)X509Certificate.java 1.27 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.math.BigInteger;
  12. import java.security.Principal;
  13. import java.security.PublicKey;
  14. import java.util.Date;
  15. /**
  16. * <p>
  17. * Abstract class for X.509 certificates. This provides a standard
  18. * way to access all the attributes of an X.509 certificate.
  19. * <p>
  20. * In June of 1996, the basic X.509 v3 format was completed by
  21. * ISO/IEC and ANSI X9, which is described below in ASN.1:
  22. * <pre>
  23. * Certificate ::= SEQUENCE {
  24. * tbsCertificate TBSCertificate,
  25. * signatureAlgorithm AlgorithmIdentifier,
  26. * signature BIT STRING }
  27. * </pre>
  28. * <p>
  29. * These certificates are widely used to support authentication and
  30. * other functionality in Internet security systems. Common applications
  31. * include Privacy Enhanced Mail (PEM), Transport Layer Security (SSL),
  32. * code signing for trusted software distribution, and Secure Electronic
  33. * Transactions (SET).
  34. * <p>
  35. * These certificates are managed and vouched for by <em>Certificate
  36. * Authorities</em> (CAs). CAs are services which create certificates by
  37. * placing data in the X.509 standard format and then digitally signing
  38. * that data. CAs act as trusted third parties, making introductions
  39. * between principals who have no direct knowledge of each other.
  40. * CA certificates are either signed by themselves, or by some other
  41. * CA such as a "root" CA.
  42. * <p>
  43. * More information can be found in RFC 2459,
  44. * "Internet X.509 Public Key Infrastructure Certificate and CRL
  45. * Profile" at <A HREF="http://www.ietf.org/rfc/rfc2459.txt">
  46. * http://www.ietf.org/rfc/rfc2459.txt </A>.
  47. * <p>
  48. * The ASN.1 definition of <code>tbsCertificate</code> is:
  49. * <pre>
  50. * TBSCertificate ::= SEQUENCE {
  51. * version [0] EXPLICIT Version DEFAULT v1,
  52. * serialNumber CertificateSerialNumber,
  53. * signature AlgorithmIdentifier,
  54. * issuer Name,
  55. * validity Validity,
  56. * subject Name,
  57. * subjectPublicKeyInfo SubjectPublicKeyInfo,
  58. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  59. * -- If present, version must be v2 or v3
  60. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  61. * -- If present, version must be v2 or v3
  62. * extensions [3] EXPLICIT Extensions OPTIONAL
  63. * -- If present, version must be v3
  64. * }
  65. * </pre>
  66. * <p>
  67. * Certificates are instantiated using a certificate factory. The following is
  68. * an example of how to instantiate an X.509 certificate:
  69. * <pre>
  70. * InputStream inStream = new FileInputStream("fileName-of-cert");
  71. * CertificateFactory cf = CertificateFactory.getInstance("X.509");
  72. * X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
  73. * inStream.close();
  74. * </pre>
  75. *
  76. * @author Hemma Prafullchandra
  77. *
  78. * @version 1.27
  79. *
  80. * @see Certificate
  81. * @see CertificateFactory
  82. * @see X509Extension
  83. */
  84. public abstract class X509Certificate extends Certificate
  85. implements X509Extension {
  86. /**
  87. * Constructor for X.509 certificates.
  88. */
  89. protected X509Certificate() {
  90. super("X.509");
  91. }
  92. /**
  93. * Checks that the certificate is currently valid. It is if
  94. * the current date and time are within the validity period given in the
  95. * certificate.
  96. * <p>
  97. * The validity period consists of two date/time values:
  98. * the first and last dates (and times) on which the certificate
  99. * is valid. It is defined in
  100. * ASN.1 as:
  101. * <pre>
  102. * validity Validity<p>
  103. * Validity ::= SEQUENCE {
  104. * notBefore CertificateValidityDate,
  105. * notAfter CertificateValidityDate }<p>
  106. * CertificateValidityDate ::= CHOICE {
  107. * utcTime UTCTime,
  108. * generalTime GeneralizedTime }
  109. * </pre>
  110. *
  111. * @exception CertificateExpiredException if the certificate has expired.
  112. * @exception CertificateNotYetValidException if the certificate is not
  113. * yet valid.
  114. */
  115. public abstract void checkValidity()
  116. throws CertificateExpiredException, CertificateNotYetValidException;
  117. /**
  118. * Checks that the given date is within the certificate's
  119. * validity period. In other words, this determines whether the
  120. * certificate would be valid at the given date/time.
  121. *
  122. * @param date the Date to check against to see if this certificate
  123. * is valid at that date/time.
  124. *
  125. * @exception CertificateExpiredException if the certificate has expired
  126. * with respect to the <code>date</code> supplied.
  127. * @exception CertificateNotYetValidException if the certificate is not
  128. * yet valid with respect to the <code>date</code> supplied.
  129. *
  130. * @see #checkValidity()
  131. */
  132. public abstract void checkValidity(Date date)
  133. throws CertificateExpiredException, CertificateNotYetValidException;
  134. /**
  135. * Gets the <code>version</code> (version number) value from the
  136. * certificate.
  137. * The ASN.1 definition for this is:
  138. * <pre>
  139. * version [0] EXPLICIT Version DEFAULT v1<p>
  140. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  141. * </pre>
  142. * @return the version number, i.e. 1, 2 or 3.
  143. */
  144. public abstract int getVersion();
  145. /**
  146. * Gets the <code>serialNumber</code> value from the certificate.
  147. * The serial number is an integer assigned by the certification
  148. * authority to each certificate. It must be unique for each
  149. * certificate issued by a given CA (i.e., the issuer name and
  150. * serial number identify a unique certificate).
  151. * The ASN.1 definition for this is:
  152. * <pre>
  153. * serialNumber CertificateSerialNumber<p>
  154. *
  155. * CertificateSerialNumber ::= INTEGER
  156. * </pre>
  157. *
  158. * @return the serial number.
  159. */
  160. public abstract BigInteger getSerialNumber();
  161. /**
  162. * Gets the <code>issuer</code> (issuer distinguished name) value from
  163. * the certificate. The issuer name identifies the entity that signed (and
  164. * issued) the certificate.
  165. *
  166. * <p>The issuer name field contains an
  167. * X.500 distinguished name (DN).
  168. * The ASN.1 definition for this is:
  169. * <pre>
  170. * issuer Name<p>
  171. *
  172. * Name ::= CHOICE { RDNSequence }
  173. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  174. * RelativeDistinguishedName ::=
  175. * SET OF AttributeValueAssertion
  176. *
  177. * AttributeValueAssertion ::= SEQUENCE {
  178. * AttributeType,
  179. * AttributeValue }
  180. * AttributeType ::= OBJECT IDENTIFIER
  181. * AttributeValue ::= ANY
  182. * </pre>
  183. * The <code>Name</code> describes a hierarchical name composed of
  184. * attributes,
  185. * such as country name, and corresponding values, such as US.
  186. * The type of the <code>AttributeValue</code> component is determined by
  187. * the <code>AttributeType</code> in general it will be a
  188. * <code>directoryString</code>. A <code>directoryString</code> is usually
  189. * one of <code>PrintableString</code>,
  190. * <code>TeletexString</code> or <code>UniversalString</code>.
  191. *
  192. * @return a Principal whose name is the issuer distinguished name.
  193. */
  194. public abstract Principal getIssuerDN();
  195. /**
  196. * Gets the <code>subject</code> (subject distinguished name) value
  197. * from the certificate.
  198. * The ASN.1 definition for this is:
  199. * <pre>
  200. * subject Name
  201. * </pre>
  202. *
  203. * <p>See {@link #getIssuerDN() getIssuerDN} for <code>Name</code>
  204. * and other relevant definitions.
  205. *
  206. * @return a Principal whose name is the subject name.
  207. */
  208. public abstract Principal getSubjectDN();
  209. /**
  210. * Gets the <code>notBefore</code> date from the validity period of
  211. * the certificate.
  212. * The relevant ASN.1 definitions are:
  213. * <pre>
  214. * validity Validity<p>
  215. *
  216. * Validity ::= SEQUENCE {
  217. * notBefore CertificateValidityDate,
  218. * notAfter CertificateValidityDate }<p>
  219. * CertificateValidityDate ::= CHOICE {
  220. * utcTime UTCTime,
  221. * generalTime GeneralizedTime }
  222. * </pre>
  223. *
  224. * @return the start date of the validity period.
  225. * @see #checkValidity
  226. */
  227. public abstract Date getNotBefore();
  228. /**
  229. * Gets the <code>notAfter</code> date from the validity period of
  230. * the certificate. See {@link #getNotBefore() getNotBefore}
  231. * for relevant ASN.1 definitions.
  232. *
  233. * @return the end date of the validity period.
  234. * @see #checkValidity
  235. */
  236. public abstract Date getNotAfter();
  237. /**
  238. * Gets the DER-encoded certificate information, the
  239. * <code>tbsCertificate</code> from this certificate.
  240. * This can be used to verify the signature independently.
  241. *
  242. * @return the DER-encoded certificate information.
  243. * @exception CertificateEncodingException if an encoding error occurs.
  244. */
  245. public abstract byte[] getTBSCertificate()
  246. throws CertificateEncodingException;
  247. /**
  248. * Gets the <code>signature</code> value (the raw signature bits) from
  249. * the certificate.
  250. * The ASN.1 definition for this is:
  251. * <pre>
  252. * signature BIT STRING
  253. * </pre>
  254. *
  255. * @return the signature.
  256. */
  257. public abstract byte[] getSignature();
  258. /**
  259. * Gets the signature algorithm name for the certificate
  260. * signature algorithm. An example is the string "SHA-1/DSA".
  261. * The ASN.1 definition for this is:
  262. * <pre>
  263. * signatureAlgorithm AlgorithmIdentifier<p>
  264. * AlgorithmIdentifier ::= SEQUENCE {
  265. * algorithm OBJECT IDENTIFIER,
  266. * parameters ANY DEFINED BY algorithm OPTIONAL }
  267. * -- contains a value of the type
  268. * -- registered for use with the
  269. * -- algorithm object identifier value
  270. * </pre>
  271. *
  272. * <p>The algorithm name is determined from the <code>algorithm</code>
  273. * OID string.
  274. *
  275. * @return the signature algorithm name.
  276. */
  277. public abstract String getSigAlgName();
  278. /**
  279. * Gets the signature algorithm OID string from the certificate.
  280. * An OID is represented by a set of positive whole numbers separated
  281. * by periods.
  282. * For example, the string "1.2.840.10040.4.3" identifies the SHA-1
  283. * with DSA signature algorithm, as per RFC 2459.
  284. *
  285. * <p>See {@link #getSigAlgName() getSigAlgName} for
  286. * relevant ASN.1 definitions.
  287. *
  288. * @return the signature algorithm OID string.
  289. */
  290. public abstract String getSigAlgOID();
  291. /**
  292. * Gets the DER-encoded signature algorithm parameters from this
  293. * certificate's signature algorithm. In most cases, the signature
  294. * algorithm parameters are null; the parameters are usually
  295. * supplied with the certificate's public key.
  296. * If access to individual parameter values is needed then use
  297. * {@link java.security.AlgorithmParameters AlgorithmParameters}
  298. * and instantiate with the name returned by
  299. * {@link #getSigAlgName() getSigAlgName}.
  300. *
  301. * <p>See {@link #getSigAlgName() getSigAlgName} for
  302. * relevant ASN.1 definitions.
  303. *
  304. * @return the DER-encoded signature algorithm parameters, or
  305. * null if no parameters are present.
  306. */
  307. public abstract byte[] getSigAlgParams();
  308. /**
  309. * Gets the <code>issuerUniqueID</code> value from the certificate.
  310. * The issuer unique identifier is present in the certificate
  311. * to handle the possibility of reuse of issuer names over time.
  312. * RFC 2459 recommends that names not be reused and that
  313. * conforming certificates not make use of unique identifiers.
  314. * Applications conforming to that profile should be capable of
  315. * parsing unique identifiers and making comparisons.
  316. *
  317. * <p>The ASN.1 definition for this is:
  318. * <pre>
  319. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL<p>
  320. * UniqueIdentifier ::= BIT STRING
  321. * </pre>
  322. *
  323. * @return the issuer unique identifier or null if it is not
  324. * present in the certificate.
  325. */
  326. public abstract boolean[] getIssuerUniqueID();
  327. /**
  328. * Gets the <code>subjectUniqueID</code> value from the certificate.
  329. *
  330. * <p>The ASN.1 definition for this is:
  331. * <pre>
  332. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL<p>
  333. * UniqueIdentifier ::= BIT STRING
  334. * </pre>
  335. *
  336. * @return the subject unique identifier or null if it is not
  337. * present in the certificate.
  338. */
  339. public abstract boolean[] getSubjectUniqueID();
  340. /**
  341. * Gets a boolean array representing bits of
  342. * the <code>KeyUsage</code> extension, (OID = 2.5.29.15).
  343. * The key usage extension defines the purpose (e.g., encipherment,
  344. * signature, certificate signing) of the key contained in the
  345. * certificate.
  346. * The ASN.1 definition for this is:
  347. * <pre>
  348. * KeyUsage ::= BIT STRING {
  349. * digitalSignature (0),
  350. * nonRepudiation (1),
  351. * keyEncipherment (2),
  352. * dataEncipherment (3),
  353. * keyAgreement (4),
  354. * keyCertSign (5),
  355. * cRLSign (6),
  356. * encipherOnly (7),
  357. * decipherOnly (8) }
  358. * </pre>
  359. * RFC 2459 recommends that when used, this be marked
  360. * as a critical extension.
  361. *
  362. * @return the KeyUsage extension of this certificate, represented as
  363. * an array of booleans. The order of KeyUsage values in the array is
  364. * the same as in the above ASN.1 definition. The array will contain a
  365. * value for each KeyUsage defined above. If the KeyUsage list encoded
  366. * in the certificate is longer than the above list, it will not be
  367. * truncated. Returns null if this certificate does not
  368. * contain a KeyUsage extension.
  369. */
  370. public abstract boolean[] getKeyUsage();
  371. /**
  372. * Gets the certificate constraints path length from the
  373. * critical <code>BasicConstraints</code> extension, (OID = 2.5.29.19).
  374. * <p>
  375. * The basic constraints extension identifies whether the subject
  376. * of the certificate is a Certificate Authority (CA) and
  377. * how deep a certification path may exist through that CA. The
  378. * <code>pathLenConstraint</code> field (see below) is meaningful
  379. * only if <code>cA</code> is set to TRUE. In this case, it gives the
  380. * maximum number of CA certificates that may follow this certificate in a
  381. * certification path. A value of zero indicates that only an end-entity
  382. * certificate may follow in the path.
  383. * <p>
  384. * Note that for RFC 2459 this extension is always marked
  385. * critical if <code>cA</code> is TRUE, meaning this certificate belongs
  386. * to a Certificate Authority.
  387. * <p>
  388. * The ASN.1 definition for this is:
  389. * <pre>
  390. * BasicConstraints ::= SEQUENCE {
  391. * cA BOOLEAN DEFAULT FALSE,
  392. * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  393. * </pre>
  394. *
  395. * @return the value of <code>pathLenConstraint</code> if the
  396. * BasicConstraints extension is present in the certificate and the
  397. * subject of the certificate is a CA, otherwise -1.
  398. * If the subject of the certificate is a CA and
  399. * <code>pathLenConstraint</code> does not appear,
  400. * <code>Integer.MAX_VALUE</code> is returned to indicate that there is no
  401. * limit to the allowed length of the certification path.
  402. */
  403. public abstract int getBasicConstraints();
  404. }