X509Certificate.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* X509Certificate.java --- X.509 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.math.BigInteger;
  33. import java.security.Principal;
  34. import java.security.PublicKey;
  35. import java.security.NoSuchAlgorithmException;
  36. import java.security.InvalidKeyException;
  37. import java.security.NoSuchProviderException;
  38. import java.security.SignatureException;
  39. import java.util.Date;
  40. /**
  41. X509Certificate is the abstract class for X.509 certificates.
  42. This provides a stanard class interface for accessing all
  43. the attributes of X.509 certificates.
  44. In June 1996, the basic X.509 v3 format was finished by
  45. ISO/IEC and ANSI X.9. The ASN.1 DER format is below:
  46. Certificate ::= SEQUENCE {
  47. tbsCertificate TBSCertificate,
  48. signatureAlgorithm AlgorithmIdentifier,
  49. signatureValue BIT STRING }
  50. These certificates are widely used in various Internet
  51. protocols to support authentication. It is used in
  52. Privacy Enhanced Mail (PEM), Transport Layer Security (TLS),
  53. Secure Sockets Layer (SSL), code signing for trusted software
  54. distribution, and Secure Electronic Transactions (SET).
  55. The certificates are managed and vouched for by
  56. <I>Certificate Authorities</I> (CAs). CAs are companies or
  57. groups that create certificates by placing the data in the
  58. X.509 certificate format and signing it with their private
  59. key. CAs serve as trusted third parties by certifying that
  60. the person or group specified in the certificate is who
  61. they say they are.
  62. The ASN.1 defintion for <I>tbsCertificate</I> is
  63. TBSCertificate ::= SEQUENCE {
  64. version [0] EXPLICIT Version DEFAULT v1,
  65. serialNumber CertificateSerialNumber,
  66. signature AlgorithmIdentifier,
  67. issuer Name,
  68. validity Validity,
  69. subject Name,
  70. subjectPublicKeyInfo SubjectPublicKeyInfo,
  71. issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  72. -- If present, version shall be v2 or v3
  73. subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  74. -- If present, version shall be v2 or v3
  75. extensions [3] EXPLICIT Extensions OPTIONAL
  76. -- If present, version shall be v3
  77. }
  78. Version ::= INTEGER { v1(0), v2(1), v3(2) }
  79. CertificateSerialNumber ::= INTEGER
  80. Validity ::= SEQUENCE {
  81. notBefore Time,
  82. notAfter Time }
  83. Time ::= CHOICE {
  84. utcTime UTCTime,
  85. generalTime GeneralizedTime }
  86. UniqueIdentifier ::= BIT STRING
  87. SubjectPublicKeyInfo ::= SEQUENCE {
  88. algorithm AlgorithmIdentifier,
  89. subjectPublicKey BIT STRING }
  90. Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  91. Extension ::= SEQUENCE {
  92. extnID OBJECT IDENTIFIER,
  93. critical BOOLEAN DEFAULT FALSE,
  94. extnValue OCTET STRING }
  95. Certificates are created with the CertificateFactory.
  96. For more information about X.509 certificates, consult
  97. rfc2459.
  98. @since JDK 1.2
  99. @author Mark Benvenuto
  100. */
  101. public abstract class X509Certificate extends Certificate implements X509Extension
  102. {
  103. /**
  104. Constructs a new certificate of the specified type.
  105. */
  106. protected X509Certificate()
  107. {
  108. super( "X.509" );
  109. }
  110. /**
  111. Checks the validity of the X.509 certificate. It is valid
  112. if the current date and time are within the period specified
  113. by the certificate.
  114. The ASN.1 DER encoding is:
  115. validity Validity,
  116. Validity ::= SEQUENCE {
  117. notBefore Time,
  118. notAfter Time }
  119. Time ::= CHOICE {
  120. utcTime UTCTime,
  121. generalTime GeneralizedTime }
  122. Consult rfc2459 for more information.
  123. @throws CertificateExpiredException if the certificate expired
  124. @throws CertificateNotYetValidException if the certificate is
  125. not yet valid
  126. */
  127. public abstract void checkValidity()
  128. throws CertificateExpiredException,
  129. CertificateNotYetValidException;
  130. /**
  131. Checks the validity of the X.509 certificate for the
  132. specified time and date. It is valid if the specified
  133. date and time are within the period specified by
  134. the certificate.
  135. @throws CertificateExpiredException if the certificate expired
  136. based on the date
  137. @throws CertificateNotYetValidException if the certificate is
  138. not yet valid based on the date
  139. */
  140. public abstract void checkValidity(Date date)
  141. throws CertificateExpiredException,
  142. CertificateNotYetValidException;
  143. /**
  144. Returns the version of this certificate.
  145. The ASN.1 DER encoding is:
  146. version [0] EXPLICIT Version DEFAULT v1,
  147. Version ::= INTEGER { v1(0), v2(1), v3(2) }
  148. Consult rfc2459 for more information.
  149. @return version number of certificate
  150. */
  151. public abstract int getVersion();
  152. /**
  153. Gets the serial number for serial Number in
  154. this Certifcate. It must be a unique number
  155. unique other serial numbers from the granting CA.
  156. The ASN.1 DER encoding is:
  157. serialNumber CertificateSerialNumber,
  158. CertificateSerialNumber ::= INTEGER
  159. Consult rfc2459 for more information.
  160. @return the serial number for this X509CRLEntry.
  161. */
  162. public abstract BigInteger getSerialNumber();
  163. /**
  164. Returns the issuer (issuer distinguished name) of the
  165. Certificate. The issuer is the entity who signed
  166. and issued the Certificate.
  167. The ASN.1 DER encoding is:
  168. issuer Name,
  169. Name ::= CHOICE {
  170. RDNSequence }
  171. RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  172. RelativeDistinguishedName ::=
  173. SET OF AttributeTypeAndValue
  174. AttributeTypeAndValue ::= SEQUENCE {
  175. type AttributeType,
  176. value AttributeValue }
  177. AttributeType ::= OBJECT IDENTIFIER
  178. AttributeValue ::= ANY DEFINED BY AttributeType
  179. DirectoryString ::= CHOICE {
  180. teletexString TeletexString (SIZE (1..MAX)),
  181. printableString PrintableString (SIZE (1..MAX)),
  182. universalString UniversalString (SIZE (1..MAX)),
  183. utf8String UTF8String (SIZE (1.. MAX)),
  184. bmpString BMPString (SIZE (1..MAX)) }
  185. Consult rfc2459 for more information.
  186. @return the issuer in the Principal class
  187. */
  188. public abstract Principal getIssuerDN();
  189. /**
  190. Returns the subject (subject distinguished name) of the
  191. Certificate. The subject is the entity who the Certificate
  192. identifies.
  193. The ASN.1 DER encoding is:
  194. subject Name,
  195. Consult rfc2459 for more information.
  196. @return the issuer in the Principal class
  197. */
  198. public abstract Principal getSubjectDN();
  199. /**
  200. Returns the date that this certificate is not to be used
  201. before, <I>notBefore</I>.
  202. The ASN.1 DER encoding is:
  203. validity Validity,
  204. Validity ::= SEQUENCE {
  205. notBefore Time,
  206. notAfter Time }
  207. Time ::= CHOICE {
  208. utcTime UTCTime,
  209. generalTime GeneralizedTime }
  210. Consult rfc2459 for more information.
  211. @return the date <I>notBefore</I>
  212. */
  213. public abstract Date getNotBefore();
  214. /**
  215. Returns the date that this certificate is not to be used
  216. after, <I>notAfter</I>.
  217. @return the date <I>notAfter</I>
  218. */
  219. public abstract Date getNotAfter();
  220. /**
  221. Returns the <I>tbsCertificate</I> from the certificate.
  222. @return the DER encoded tbsCertificate
  223. @throws CertificateEncodingException if encoding error occurred
  224. */
  225. public abstract byte[] getTBSCertificate() throws CertificateEncodingException;
  226. /**
  227. Returns the signature in its raw DER encoded format.
  228. The ASN.1 DER encoding is:
  229. signatureValue BIT STRING
  230. Consult rfc2459 for more information.
  231. @return byte array representing signature
  232. */
  233. public abstract byte[] getSignature();
  234. /**
  235. Returns the signature algorithm used to sign the CRL.
  236. An examples is "SHA-1/DSA".
  237. The ASN.1 DER encoding is:
  238. signatureAlgorithm AlgorithmIdentifier,
  239. AlgorithmIdentifier ::= SEQUENCE {
  240. algorithm OBJECT IDENTIFIER,
  241. parameters ANY DEFINED BY algorithm OPTIONAL }
  242. Consult rfc2459 for more information.
  243. The algorithm name is determined from the OID.
  244. @return a string with the signature algorithm name
  245. */
  246. public abstract String getSigAlgName();
  247. /**
  248. Returns the OID for the signature algorithm used.
  249. Example "1.2.840.10040.4.3" is return for SHA-1 with DSA.\
  250. The ASN.1 DER encoding for the example is:
  251. id-dsa-with-sha1 ID ::= {
  252. iso(1) member-body(2) us(840) x9-57 (10040)
  253. x9cm(4) 3 }
  254. Consult rfc2459 for more information.
  255. @return a string containing the OID.
  256. */
  257. public abstract String getSigAlgOID();
  258. /**
  259. Returns the AlgorithmParameters in the encoded form
  260. for the signature algorithm used.
  261. If access to the parameters is need, create an
  262. instance of AlgorithmParameters.
  263. @return byte array containing algorithm parameters, null
  264. if no parameters are present in certificate
  265. */
  266. public abstract byte[] getSigAlgParams();
  267. /**
  268. Returns the issuer unique ID for this certificate.
  269. The ASN.1 DER encoding is:
  270. issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  271. -- If present, version shall be v2 or v3
  272. UniqueIdentifier ::= BIT STRING
  273. Consult rfc2459 for more information.
  274. @return bit representation of <I>issuerUniqueID</I>
  275. */
  276. public abstract boolean[] getIssuerUniqueID();
  277. /**
  278. Returns the subject unique ID for this certificate.
  279. The ASN.1 DER encoding is:
  280. subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  281. -- If present, version shall be v2 or v3
  282. UniqueIdentifier ::= BIT STRING
  283. Consult rfc2459 for more information.
  284. @return bit representation of <I>subjectUniqueID</I>
  285. */
  286. public abstract boolean[] getSubjectUniqueID();
  287. /**
  288. Returns a boolean array representing the <I>KeyUsage</I>
  289. extension for the certificate. The KeyUsage (OID = 2.5.29.15)
  290. defines the purpose of the key in the certificate.
  291. The ASN.1 DER encoding is:
  292. id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
  293. KeyUsage ::= BIT STRING {
  294. digitalSignature (0),
  295. nonRepudiation (1),
  296. keyEncipherment (2),
  297. dataEncipherment (3),
  298. keyAgreement (4),
  299. keyCertSign (5),
  300. cRLSign (6),
  301. encipherOnly (7),
  302. decipherOnly (8) }
  303. Consult rfc2459 for more information.
  304. @return bit representation of <I>KeyUsage</I>
  305. */
  306. public abstract boolean[] getKeyUsage();
  307. /**
  308. Returns the certificate constraints path length from the
  309. critical BasicConstraints extension, (OID = 2.5.29.19).
  310. The basic constraints extensions is used to determine if
  311. the subject of the certificate is a Certificate Authority (CA)
  312. and how deep the certification path may exist. The
  313. <I>pathLenConstraint</I> only takes affect if <I>cA</I>
  314. is set to true. "A value of zero indicates that only an
  315. end-entity certificate may follow in the path." (rfc2459)
  316. The ASN.1 DER encoding is:
  317. id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
  318. BasicConstraints ::= SEQUENCE {
  319. cA BOOLEAN DEFAULT FALSE,
  320. pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  321. Consult rfc2459 for more information.
  322. @return the length of the path constraint if BasicConstraints
  323. is present and cA is TRUE. Otherwise returns -1.
  324. */
  325. public abstract int getBasicConstraints();
  326. }