001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.ASN1Set;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DERObjectIdentifier;
008: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
009: import org.bouncycastle.asn1.pkcs.SignedData;
010: import org.bouncycastle.asn1.x509.CertificateList;
011: import org.bouncycastle.util.StreamParsingException;
012: import org.bouncycastle.x509.X509StreamParserSpi;
013:
014: import java.io.BufferedInputStream;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.security.cert.CRL;
018: import java.security.cert.CRLException;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.List;
022:
023: public class X509CRLParser extends X509StreamParserSpi {
024: private static final PEMUtil PEM_PARSER = new PEMUtil("CRL");
025:
026: private ASN1Set sData = null;
027: private int sDataObjectCount = 0;
028: private InputStream currentStream = null;
029:
030: private CRL readDERCRL(InputStream in) throws IOException,
031: CRLException {
032: ASN1InputStream dIn = new ASN1InputStream(in, ProviderUtil
033: .getReadLimit(in));
034: ASN1Sequence seq = (ASN1Sequence) dIn.readObject();
035:
036: if (seq.size() > 1
037: && seq.getObjectAt(0) instanceof DERObjectIdentifier) {
038: if (seq.getObjectAt(0).equals(
039: PKCSObjectIdentifiers.signedData)) {
040: sData = new SignedData(ASN1Sequence.getInstance(
041: (ASN1TaggedObject) seq.getObjectAt(1), true))
042: .getCRLs();
043:
044: return getCRL();
045: }
046: }
047:
048: return new X509CRLObject(CertificateList.getInstance(seq));
049: }
050:
051: private CRL getCRL() throws CRLException {
052: if (sData == null || sDataObjectCount >= sData.size()) {
053: return null;
054: }
055:
056: return new X509CRLObject(CertificateList.getInstance(sData
057: .getObjectAt(sDataObjectCount++)));
058: }
059:
060: private CRL readPEMCRL(InputStream in) throws IOException,
061: CRLException {
062: ASN1Sequence seq = PEM_PARSER.readPEMObject(in);
063:
064: if (seq != null) {
065: return new X509CRLObject(CertificateList.getInstance(seq));
066: }
067:
068: return null;
069: }
070:
071: public void engineInit(InputStream in) {
072: currentStream = in;
073: sData = null;
074: sDataObjectCount = 0;
075:
076: if (!currentStream.markSupported()) {
077: currentStream = new BufferedInputStream(currentStream);
078: }
079: }
080:
081: public Object engineRead() throws StreamParsingException {
082: try {
083: if (sData != null) {
084: if (sDataObjectCount != sData.size()) {
085: return getCRL();
086: } else {
087: sData = null;
088: sDataObjectCount = 0;
089: return null;
090: }
091: }
092:
093: currentStream.mark(10);
094: int tag = currentStream.read();
095:
096: if (tag == -1) {
097: return null;
098: }
099:
100: if (tag != 0x30) // assume ascii PEM encoded.
101: {
102: currentStream.reset();
103: return readPEMCRL(currentStream);
104: } else {
105: currentStream.reset();
106: return readDERCRL(currentStream);
107: }
108: } catch (Exception e) {
109: throw new StreamParsingException(e.toString(), e);
110: }
111: }
112:
113: public Collection engineReadAll() throws StreamParsingException {
114: CRL crl;
115: List certs = new ArrayList();
116:
117: while ((crl = (CRL) engineRead()) != null) {
118: certs.add(crl);
119: }
120:
121: return certs;
122: }
123: }
|