01: package org.bouncycastle.jce.provider;
02:
03: import org.bouncycastle.asn1.ASN1InputStream;
04: import org.bouncycastle.asn1.ASN1Sequence;
05: import org.bouncycastle.asn1.x509.CertificatePair;
06: import org.bouncycastle.util.StreamParsingException;
07: import org.bouncycastle.x509.X509CertificatePair;
08: import org.bouncycastle.x509.X509StreamParserSpi;
09:
10: import java.io.BufferedInputStream;
11: import java.io.IOException;
12: import java.io.InputStream;
13: import java.security.cert.CertificateParsingException;
14: import java.util.ArrayList;
15: import java.util.Collection;
16: import java.util.List;
17:
18: public class X509CertPairParser extends X509StreamParserSpi {
19: private InputStream currentStream = null;
20:
21: private X509CertificatePair readDERCrossCertificatePair(
22: InputStream in) throws IOException,
23: CertificateParsingException {
24: ASN1InputStream dIn = new ASN1InputStream(in, ProviderUtil
25: .getReadLimit(in));
26: ASN1Sequence seq = (ASN1Sequence) dIn.readObject();
27: CertificatePair pair = CertificatePair.getInstance(seq);
28: return new X509CertificatePair(pair);
29: }
30:
31: public void engineInit(InputStream in) {
32: currentStream = in;
33:
34: if (!currentStream.markSupported()) {
35: currentStream = new BufferedInputStream(currentStream);
36: }
37: }
38:
39: public Object engineRead() throws StreamParsingException {
40: try {
41:
42: currentStream.mark(10);
43: int tag = currentStream.read();
44:
45: if (tag == -1) {
46: return null;
47: }
48:
49: currentStream.reset();
50: return readDERCrossCertificatePair(currentStream);
51: } catch (Exception e) {
52: throw new StreamParsingException(e.toString(), e);
53: }
54: }
55:
56: public Collection engineReadAll() throws StreamParsingException {
57: X509CertificatePair pair;
58: List certs = new ArrayList();
59:
60: while ((pair = (X509CertificatePair) engineRead()) != null) {
61: certs.add(pair);
62: }
63:
64: return certs;
65: }
66: }
|