001: package org.bouncycastle.jce.provider.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.security.Security;
005: import java.security.cert.CertificateFactory;
006: import java.security.cert.X509CRL;
007: import java.security.cert.X509Certificate;
008:
009: import org.bouncycastle.jce.provider.BouncyCastleProvider;
010: import org.bouncycastle.util.encoders.Hex;
011: import org.bouncycastle.util.test.SimpleTestResult;
012: import org.bouncycastle.util.test.Test;
013: import org.bouncycastle.util.test.TestResult;
014:
015: public class PKIXTest implements Test {
016: /*
017: * The following certs and crls are described in:
018: * http://www.ietf.org/internet-drafts/draft-ietf-pkix-new-part1-08.txt
019: *
020: * This section contains four examples: three certificates and a CRL.
021: * The first two certificates and the CRL comprise a minimal
022: * certification path.
023: *
024: * Section C.1 contains an annotated hex dump of a "self-signed"
025: * certificate issued by a CA whose distinguished name is
026: * cn=us,o=gov,ou=nist. The certificate contains a DSA public key with
027: * parameters, and is signed by the corresponding DSA private key.
028: *
029: * Section C.2 contains an annotated hex dump of an end entity
030: * certificate. The end entity certificate contains a DSA public key,
031: * and is signed by the private key corresponding to the "self-signed"
032: * certificate in section C.1.
033: *
034: * Section C.3 contains a dump of an end entity certificate which
035: * contains an RSA public key and is signed with RSA and MD5. This
036: * certificate is not part of the minimal certification path.
037: *
038: * Section C.4 contains an annotated hex dump of a CRL. The CRL is
039: * issued by the CA whose distinguished name is cn=us,o=gov,ou=nist and
040: * the list of revoked certificates includes the end entity certificate
041: * presented in C.2.
042: */
043:
044: /**
045: * C.1 Certificate
046: *
047: * This section contains an annotated hex dump of a 699 byte version 3
048: * certificate. The certificate contains the following information:
049: * (a) the serial number is 23 (17 hex);
050: * (b) the certificate is signed with DSA and the SHA-1 hash algorithm;
051: * (c) the issuer's distinguished name is OU=NIST; O=gov; C=US
052: * (d) and the subject's distinguished name is OU=NIST; O=gov; C=US
053: * (e) the certificate was issued on June 30, 1997 and will expire on
054: * December 31, 1997;
055: * (f) the certificate contains a 1024 bit DSA public key with
056: * parameters;
057: * (g) the certificate contains a subject key identifier extension
058: * generated using method (1) of section 4.2.1.2; and
059: * (h) the certificate is a CA certificate (as indicated through the
060: * basic constraints extension.)
061: */
062: static byte[] rootCertBin = Hex
063: .decode("308202bb3082027ba003020102020111300906072a8648ce380403302a310b30"
064: + "09060355040613025553310c300a060355040a1303676f76310d300b06035504"
065: + "0b13044e495354301e170d3937303633303030303030305a170d393731323331"
066: + "3030303030305a302a310b3009060355040613025553310c300a060355040a13"
067: + "03676f76310d300b060355040b13044e495354308201b83082012c06072a8648"
068: + "ce3804013082011f02818100b68b0f942b9acea525c6f2edfcfb9532ac011233"
069: + "b9e01cad909bbc48549ef394773c2c713555e6fe4f22cbd5d83e8993334dfcbd"
070: + "4f41643ea29870ec31b450deebf198280ac93e44b3fd22979683d018a3e3bd35"
071: + "5bffeea321726a7b96dab93f1e5a90af24d620f00d21a7d402b91afcac21fb9e"
072: + "949e4b42459e6ab24863fe43021500b20db0b101df0c6624fc1392ba55f77d57"
073: + "7481e5028181009abf46b1f53f443dc9a565fb91c08e47f10ac30147c2444236"
074: + "a99281de57c5e0688658007b1ff99b77a1c510a580917851513cf6fcfccc46c6"
075: + "817892843df4933d0c387e1a5b994eab1464f60c21224e28089c92b9669f40e8"
076: + "95f6d5312aef39a262c7b26d9e58c43aa81181846daff8b419b4c211aed0223b"
077: + "aa207fee1e57180381850002818100b59e1f490447d1dbf53addca0475e8dd75"
078: + "f69b8ab197d6596982d3034dfd3b365f4af2d14ec107f5d12ad378776356ea96"
079: + "614d420b7a1dfbab91a4cedeef77c8e5ef20aea62848afbe69c36aa530f2c2b9"
080: + "d9822b7dd9c4841fde0de854d71b992eb3d088f6d6639ba7e20e82d43b8a681b"
081: + "065631590b49eb99a5d581417bc955a3323030301d0603551d0e0416041486ca"
082: + "a5228162efad0a89bcad72412c2949f48656300f0603551d130101ff04053003"
083: + "0101ff300906072a8648ce380403032f00302c0214431bcf292545c04e52e77d"
084: + "d6fcb1664c83cf2d7702140b5b9a241198e8f3869004f608a9e18da5cc3ad4");
085:
086: /**
087: * C.2 Certificate
088: *
089: * This section contains an annotated hex dump of a 730 byte version 3
090: * certificate. The certificate contains the following information:
091: * (a the serial number is 18 (12 hex);
092: * (b) the certificate is signed with DSA and the SHA-1 hash algorithm;
093: * (c) the issuer's distinguished name is OU=nist; O=gov; C=US
094: * (d) and the subject's distinguished name is CN=Tim Polk; OU=nist;
095: * O=gov; C=US
096: * (e) the certificate was valid from July 30, 1997 through December 1,
097: * 1997;
098: * (f) the certificate contains a 1024 bit DSA public key;
099: * (g) the certificate is an end entity certificate, as the basic
100: * constraints extension is not present;
101: * (h) the certificate contains an authority key identifier extension
102: * matching the subject key identifier of the certificate in Appendix
103: * C.1; and
104: * (i) the certificate includes one alternative name - an RFC 822
105: * address of "wpolk@nist.gov".
106: */
107: static byte[] userCert1Bin = Hex
108: .decode("308202da30820299a003020102020112300906072a8648ce380403302a310b30"
109: + "09060355040613025553310c300a060355040a1303676f76310d300b06035504"
110: + "0b13044e495354301e170d3937303733303030303030305a170d393731323031"
111: + "3030303030305a303d310b3009060355040613025553310c300a060355040a13"
112: + "03676f76310d300b060355040b13044e4953543111300f060355040313085469"
113: + "6d20506f6c6b308201b73082012c06072a8648ce3804013082011f02818100b6"
114: + "8b0f942b9acea525c6f2edfcfb9532ac011233b9e01cad909bbc48549ef39477"
115: + "3c2c713555e6fe4f22cbd5d83e8993334dfcbd4f41643ea29870ec31b450deeb"
116: + "f198280ac93e44b3fd22979683d018a3e3bd355bffeea321726a7b96dab93f1e"
117: + "5a90af24d620f00d21a7d402b91afcac21fb9e949e4b42459e6ab24863fe4302"
118: + "1500b20db0b101df0c6624fc1392ba55f77d577481e5028181009abf46b1f53f"
119: + "443dc9a565fb91c08e47f10ac30147c2444236a99281de57c5e0688658007b1f"
120: + "f99b77a1c510a580917851513cf6fcfccc46c6817892843df4933d0c387e1a5b"
121: + "994eab1464f60c21224e28089c92b9669f40e895f6d5312aef39a262c7b26d9e"
122: + "58c43aa81181846daff8b419b4c211aed0223baa207fee1e5718038184000281"
123: + "8030b675f77c2031ae38bb7e0d2baba09c4bdf20d524133ccd98e55f6cb7c1ba"
124: + "4abaa9958053f00d72dc3337f4010bf5041f9d2e1f62d8843a9b25095a2dc846"
125: + "8e2bd4f50d3bc72dc66cb998c1253a444e8eca9561357cce15315c23131ea205"
126: + "d17a241ccbd3720990ff9b9d28c0a10aec469f0db8d0dcd018a62b5ef98fb595"
127: + "bea33e303c30190603551d1104123010810e77706f6c6b406e6973742e676f76"
128: + "301f0603551d2304183016801486caa5228162efad0a89bcad72412c2949f486"
129: + "56300906072a8648ce380403033000302d02143697cbe3b42ce1bb61a9d3cc24"
130: + "cc22929ff4f587021500abc979afd2161ca9e368a91410b4a02eff225a73");
131:
132: /**
133: * C.3 End Entity Certificate Using RSA
134: *
135: * This section contains an annotated hex dump of a 654 byte version 3
136: * certificate. The certificate contains the following information:
137: * (a) the serial number is 256;
138: * (b) the certificate is signed with RSA and the SHA-1 hash algorithm;
139: * (c) the issuer's distinguished name is OU=NIST; O=gov; C=US
140: * (d) and the subject's distinguished name is CN=Tim Polk; OU=NIST;
141: * O=gov; C=US
142: * (e) the certificate was issued on May 21, 1996 at 09:58:26 and
143: * expired on May 21, 1997 at 09:58:26;
144: * (f) the certificate contains a 1024 bit RSA public key;
145: * (g) the certificate is an end entity certificate (not a CA
146: * certificate);
147: * (h) the certificate includes an alternative subject name of
148: * "<http://www.itl.nist.gov/div893/staff/polk/index.html>" and an
149: * alternative issuer name of "<http://www.nist.gov/>" - both are URLs;
150: * (i) the certificate include an authority key identifier extension
151: * and a certificate policies extension psecifying the policy OID
152: * 2.16.840.1.101.3.2.1.48.9; and
153: * (j) the certificate includes a critical key usage extension
154: * specifying that the public key is intended for verification of
155: * digital signatures.
156: */
157: static byte[] userCert2Bin = Hex
158: .decode("3082028e308201f7a00302010202020100300d06092a864886f70d0101050500"
159: + "302a310b3009060355040613025553310c300a060355040b1303676f76310d30"
160: + "0b060355040a13044e495354301e170d3936303532313039353832365a170d39"
161: + "37303532313039353832365a303d310b3009060355040613025553310c300a06"
162: + "0355040b1303676f76310d300b060355040a13044e4953543111300f06035504"
163: + "03130854696d20506f6c6b30819f300d06092a864886f70d010101050003818d"
164: + "0030818902818100e16ae4033097023cf410f3b51e4d7f147bf6f5d078e9a48a"
165: + "f0a375ecedb656967f8899859af23e687787eb9ed19fc0b417dcab8923a41d7e"
166: + "16234c4fa84df531b87caae31a4909f44b26db2767308212014ae91ab6c10c53"
167: + "8b6cfc2f7a43ec33367e32b27bd5aacf0114c612ec13f22d147a8b215814134c"
168: + "46a39af21695ff230203010001a381af3081ac303f0603551d11043830368634"
169: + "687474703a2f2f7777772e69746c2e6e6973742e676f762f6469763839332f73"
170: + "746166662f706f6c6b2f696e6465782e68746d6c301f0603551d120418301686"
171: + "14687474703a2f2f7777772e6e6973742e676f762f301f0603551d2304183016"
172: + "80140868af8533c8394a7af882938e706a4a20842c3230170603551d20041030"
173: + "0e300c060a60864801650302013009300e0603551d0f0101ff04040302078030"
174: + "0d06092a864886f70d0101050500038181008e8e3656788bbfa13975172ee310"
175: + "dc832b6834521cf66c1d525e5420105e4ca940f94b729e82b961dceb32a5bdb1"
176: + "b148f99b01bbebaf9b83f6528cb06d7cd09a39543e6d206fcdd0debe275f204f"
177: + "b6ab0df5b7e1bab4dfdf3dd4f6ed01fb6ecb9859ac41fb489c1ff65b46e029e2"
178: + "76ecc43a0afc92c5c0d2a9c9d32952876533");
179:
180: /**
181: * This section contains an annotated hex dump of a version 2 CRL with
182: * one extension (cRLNumber). The CRL was issued by OU=NIST; O=gov; C=US
183: * on August 7, 1997; the next scheduled issuance was September 7, 1997.
184: * The CRL includes one revoked certificates: serial number 18 (12 hex),
185: * which was revoked on July 31, 1997 due to keyCompromise. The CRL
186: * itself is number 18, and it was signed with DSA and SHA-1.
187: */
188: static byte[] crlBin = Hex
189: .decode("3081cb30818c020101300906072a8648ce380403302a310b3009060355040613025553310c300a060355040a1303676f76310d300b060355040b13044e495354170d3937303830373030303030305a170d3937303930373030303030305a30223020020112170d3937303733313030303030305a300c300a0603551d1504030a0101a00e300c300a0603551d14040302010c300906072a8648ce380403032f00302c0214224e9f43ba950634f2bb5e65dba68005c03a29470214591a57c982d7022114c3d40b321b9616b11f465a");
190:
191: public TestResult perform() {
192: try {
193: CertificateFactory cf = CertificateFactory.getInstance(
194: "X.509", "BC");
195:
196: X509Certificate rootCert = (X509Certificate) cf
197: .generateCertificate(new ByteArrayInputStream(
198: rootCertBin));
199: X509Certificate userCert1 = (X509Certificate) cf
200: .generateCertificate(new ByteArrayInputStream(
201: userCert1Bin));
202: X509Certificate userCert2 = (X509Certificate) cf
203: .generateCertificate(new ByteArrayInputStream(
204: userCert2Bin));
205: X509CRL crl = (X509CRL) cf
206: .generateCRL(new ByteArrayInputStream(crlBin));
207: rootCert.verify(rootCert.getPublicKey(), "BC");
208: userCert1.verify(rootCert.getPublicKey(), "BC");
209:
210: crl.verify(rootCert.getPublicKey(), "BC");
211:
212: if (!crl.isRevoked(userCert1)) {
213: return new SimpleTestResult(false, this .getName()
214: + ": usercert1 not revoked.");
215: }
216:
217: if (crl.isRevoked(userCert2)) {
218: return new SimpleTestResult(false, this .getName()
219: + ": usercert2 revoked.");
220: }
221:
222: } catch (Exception e) {
223: return new SimpleTestResult(false, this .getName()
224: + ": exception - " + e.toString());
225: }
226:
227: return new SimpleTestResult(true, this .getName() + ": Okay");
228: }
229:
230: public String getName() {
231: return "PKIX";
232: }
233:
234: public static void main(String[] args) {
235: Security.addProvider(new BouncyCastleProvider());
236:
237: Test test = new PKIXTest();
238: TestResult result = test.perform();
239:
240: System.out.println(result.toString());
241: }
242:
243: }
|