001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.asn1.ASN1EncodableVector;
004: import org.bouncycastle.asn1.ASN1InputStream;
005: import org.bouncycastle.asn1.DERSet;
006: import org.bouncycastle.asn1.DERTaggedObject;
007: import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
008: import org.bouncycastle.asn1.cms.ContentInfo;
009: import org.bouncycastle.asn1.cms.SignedData;
010: import org.bouncycastle.jce.provider.BouncyCastleProvider;
011: import org.bouncycastle.util.encoders.Base64;
012: import org.bouncycastle.util.test.SimpleTest;
013: import org.bouncycastle.x509.X509AttributeCertificate;
014: import org.bouncycastle.x509.X509CertificatePair;
015: import org.bouncycastle.x509.X509StreamParser;
016:
017: import java.io.ByteArrayInputStream;
018: import java.io.ByteArrayOutputStream;
019: import java.security.Security;
020: import java.security.cert.X509CRL;
021: import java.security.cert.X509Certificate;
022: import java.util.Collection;
023:
024: public class X509StreamParserTest extends SimpleTest {
025: byte[] attrCert = Base64
026: .decode("MIIHQDCCBqkCAQEwgZChgY2kgYowgYcxHDAaBgkqhkiG9w0BCQEWDW1sb3JjaEB2"
027: + "dC5lZHUxHjAcBgNVBAMTFU1hcmt1cyBMb3JjaCAobWxvcmNoKTEbMBkGA1UECxMS"
028: + "VmlyZ2luaWEgVGVjaCBVc2VyMRAwDgYDVQQLEwdDbGFzcyAyMQswCQYDVQQKEwJ2"
029: + "dDELMAkGA1UEBhMCVVMwgYmkgYYwgYMxGzAZBgkqhkiG9w0BCQEWDHNzaGFoQHZ0"
030: + "LmVkdTEbMBkGA1UEAxMSU3VtaXQgU2hhaCAoc3NoYWgpMRswGQYDVQQLExJWaXJn"
031: + "aW5pYSBUZWNoIFVzZXIxEDAOBgNVBAsTB0NsYXNzIDExCzAJBgNVBAoTAnZ0MQsw"
032: + "CQYDVQQGEwJVUzANBgkqhkiG9w0BAQQFAAIBBTAiGA8yMDAzMDcxODE2MDgwMloY"
033: + "DzIwMDMwNzI1MTYwODAyWjCCBU0wggVJBg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aGIkjHK3CqBABnfSayr3"
064: + "Rd1yBoDdEv8Qb+3eEPH6ab9021AsLEnJ6LWTmybbOpMNZ3tv");
065:
066: public void performTest() throws Exception {
067: X509StreamParser parser = X509StreamParser.getInstance(
068: "Certificate", "BC");
069:
070: parser.init(new ByteArrayInputStream(CertPathTest.rootCertBin));
071: X509Certificate rootCert = (X509Certificate) parser.read();
072:
073: parser = X509StreamParser.getInstance("CRL", "BC");
074:
075: parser.init(new ByteArrayInputStream(CertPathTest.rootCrlBin));
076:
077: X509CRL rootCrl = (X509CRL) parser.read();
078:
079: parser = X509StreamParser.getInstance("AttributeCertificate",
080: "BC");
081:
082: parser.init(new ByteArrayInputStream(attrCert));
083:
084: X509AttributeCertificate aCert = (X509AttributeCertificate) parser
085: .read();
086:
087: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
088:
089: bOut.write(CertPathTest.rootCertBin);
090: bOut.write(CertPathTest.interCertBin);
091: bOut.write(CertPathTest.finalCertBin);
092:
093: parser = X509StreamParser.getInstance("Certificate", "BC");
094:
095: parser.init(bOut.toByteArray());
096:
097: Collection res = parser.readAll();
098:
099: if (res.size() != 3) {
100: fail("wrong number of certificates found");
101: }
102:
103: bOut = new ByteArrayOutputStream();
104:
105: bOut.write(CertPathTest.rootCrlBin);
106: bOut.write(CertPathTest.interCrlBin);
107:
108: parser = X509StreamParser.getInstance("CRL", "BC");
109:
110: parser.init(bOut.toByteArray());
111:
112: res = parser.readAll();
113:
114: if (res.size() != 2) {
115: fail("wrong number of CRLs found");
116: }
117:
118: bOut = new ByteArrayOutputStream();
119:
120: bOut.write(attrCert);
121: bOut.write(attrCert);
122:
123: parser = X509StreamParser.getInstance("AttributeCertificate",
124: "BC");
125:
126: parser.init(bOut.toByteArray());
127:
128: res = parser.readAll();
129:
130: if (res.size() != 2) {
131: fail("wrong number of Attribute Certificates found");
132: }
133:
134: //
135: // PEM tests
136: //
137: parser = X509StreamParser.getInstance("Certificate", "BC");
138:
139: parser.init(PEMData.CERTIFICATE_1.getBytes("US-ASCII"));
140:
141: res = parser.readAll();
142:
143: if (res.size() != 1) {
144: fail("wrong number of Certificates found");
145: }
146:
147: parser = X509StreamParser.getInstance("Certificate", "BC");
148:
149: parser.init(PEMData.CERTIFICATE_2.getBytes("US-ASCII"));
150:
151: res = parser.readAll();
152:
153: if (res.size() != 1) {
154: fail("wrong number of Certificates found");
155: }
156:
157: parser = X509StreamParser.getInstance("CRL", "BC");
158:
159: parser.init(PEMData.CRL_1.getBytes("US-ASCII"));
160:
161: res = parser.readAll();
162:
163: if (res.size() != 1) {
164: fail("wrong number of CRLs found");
165: }
166:
167: parser = X509StreamParser.getInstance("CRL", "BC");
168:
169: parser.init(PEMData.CRL_2.getBytes("US-ASCII"));
170:
171: res = parser.readAll();
172:
173: if (res.size() != 1) {
174: fail("wrong number of CRLs found");
175: }
176:
177: parser = X509StreamParser.getInstance("AttributeCertificate",
178: "BC");
179:
180: parser.init(PEMData.ATTRIBUTE_CERTIFICATE_1
181: .getBytes("US-ASCII"));
182:
183: res = parser.readAll();
184:
185: if (res.size() != 1) {
186: fail("wrong number of Attribute Certificates found");
187: }
188:
189: parser = X509StreamParser.getInstance("AttributeCertificate",
190: "BC");
191:
192: parser.init(PEMData.ATTRIBUTE_CERTIFICATE_2
193: .getBytes("US-ASCII"));
194:
195: res = parser.readAll();
196:
197: if (res.size() != 1) {
198: fail("wrong number of Attribute Certificates found");
199: }
200:
201: ASN1EncodableVector certs = new ASN1EncodableVector();
202:
203: certs.add(new ASN1InputStream(CertPathTest.rootCertBin)
204: .readObject());
205: certs.add(new DERTaggedObject(false, 2, new ASN1InputStream(
206: attrCert).readObject()));
207:
208: ASN1EncodableVector crls = new ASN1EncodableVector();
209:
210: crls.add(new ASN1InputStream(CertPathTest.rootCrlBin)
211: .readObject());
212:
213: //
214: // cross certificate pairs
215: //
216: parser = X509StreamParser.getInstance("CertificatePair", "BC");
217:
218: parser.init(new X509CertificatePair(rootCert, rootCert)
219: .getEncoded());
220:
221: res = parser.readAll();
222:
223: if (res.size() != 1) {
224: fail("wrong number of CertificatePairs found");
225: }
226:
227: //
228: // PKCS7
229: //
230: SignedData sigData = new SignedData(new DERSet(),
231: new ContentInfo(CMSObjectIdentifiers.data, null),
232: new DERSet(certs), new DERSet(crls), new DERSet());
233:
234: ContentInfo info = new ContentInfo(
235: CMSObjectIdentifiers.signedData, sigData);
236:
237: parser = X509StreamParser.getInstance("Certificate", "BC");
238:
239: parser.init(info.getEncoded());
240:
241: res = parser.readAll();
242:
243: if (res.size() != 1) {
244: fail("wrong number of Certificates found");
245: }
246:
247: parser = X509StreamParser.getInstance("CRL", "BC");
248:
249: parser.init(info.getEncoded());
250:
251: res = parser.readAll();
252:
253: if (res.size() != 1) {
254: fail("wrong number of CRLs found");
255: }
256:
257: parser = X509StreamParser.getInstance("AttributeCertificate",
258: "BC");
259:
260: parser.init(info.getEncoded());
261:
262: res = parser.readAll();
263:
264: if (res.size() != 1) {
265: fail("wrong number of Attribute Certificates found");
266: }
267:
268: // data with no certificates or CRLs
269:
270: sigData = new SignedData(new DERSet(), new ContentInfo(
271: CMSObjectIdentifiers.data, null), new DERSet(),
272: new DERSet(), new DERSet());
273:
274: info = new ContentInfo(CMSObjectIdentifiers.signedData, sigData);
275:
276: parser = X509StreamParser.getInstance("Certificate", "BC");
277:
278: parser.init(info.getEncoded());
279:
280: res = parser.readAll();
281:
282: if (res.size() != 0) {
283: fail("wrong number of Certificates found - expected 0");
284: }
285:
286: parser = X509StreamParser.getInstance("CRL", "BC");
287:
288: parser.init(info.getEncoded());
289:
290: res = parser.readAll();
291:
292: if (res.size() != 0) {
293: fail("wrong number of CRLs found - expected 0");
294: }
295:
296: parser = X509StreamParser.getInstance("AttributeCertificate",
297: "BC");
298:
299: parser.init(info.getEncoded());
300:
301: res = parser.readAll();
302:
303: if (res.size() != 0) {
304: fail("wrong number of Attribute Certificates found - expected 0");
305: }
306:
307: // data with absent certificates and CRLs
308: sigData = new SignedData(new DERSet(), new ContentInfo(
309: CMSObjectIdentifiers.data, null), null, null,
310: new DERSet());
311:
312: info = new ContentInfo(CMSObjectIdentifiers.signedData, sigData);
313:
314: parser = X509StreamParser.getInstance("Certificate", "BC");
315:
316: parser.init(info.getEncoded());
317:
318: res = parser.readAll();
319:
320: if (res.size() != 0) {
321: fail("wrong number of Certificates found - expected 0");
322: }
323:
324: parser = X509StreamParser.getInstance("CRL", "BC");
325:
326: parser.init(info.getEncoded());
327:
328: res = parser.readAll();
329:
330: if (res.size() != 0) {
331: fail("wrong number of CRLs found - expected 0");
332: }
333:
334: parser = X509StreamParser.getInstance("AttributeCertificate",
335: "BC");
336:
337: parser.init(info.getEncoded());
338:
339: res = parser.readAll();
340:
341: if (res.size() != 0) {
342: fail("wrong number of Attribute Certificates found - expected 0");
343: }
344: }
345:
346: public String getName() {
347: return "X509StreamParser";
348: }
349:
350: public static void main(String[] args) {
351: Security.addProvider(new BouncyCastleProvider());
352:
353: runTest(new X509StreamParserTest());
354: }
355:
356: }
|