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