001: package org.bouncycastle.asn1.cms;
002:
003: import org.bouncycastle.asn1.DERInteger;
004: import org.bouncycastle.asn1.ASN1SequenceParser;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1SetParser;
007: import org.bouncycastle.asn1.ASN1Set;
008: import org.bouncycastle.asn1.ASN1TaggedObjectParser;
009: import org.bouncycastle.asn1.DERTags;
010:
011: import java.io.IOException;
012:
013: /**
014: * <pre>
015: * SignedData ::= SEQUENCE {
016: * version CMSVersion,
017: * digestAlgorithms DigestAlgorithmIdentifiers,
018: * encapContentInfo EncapsulatedContentInfo,
019: * certificates [0] IMPLICIT CertificateSet OPTIONAL,
020: * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
021: * signerInfos SignerInfos
022: * }
023: * </pre>
024: */
025: public class SignedDataParser {
026: private ASN1SequenceParser _seq;
027: private DERInteger _version;
028: private Object _nextObject;
029: private boolean _certsCalled;
030: private boolean _crlsCalled;
031:
032: public static SignedDataParser getInstance(Object o)
033: throws IOException {
034: if (o instanceof ASN1Sequence) {
035: return new SignedDataParser(((ASN1Sequence) o).parser());
036: }
037: if (o instanceof ASN1SequenceParser) {
038: return new SignedDataParser((ASN1SequenceParser) o);
039: }
040:
041: throw new IOException("unknown object encountered: "
042: + o.getClass().getName());
043: }
044:
045: private SignedDataParser(ASN1SequenceParser seq) throws IOException {
046: this ._seq = seq;
047: this ._version = (DERInteger) seq.readObject();
048: }
049:
050: public DERInteger getVersion() {
051: return _version;
052: }
053:
054: public ASN1SetParser getDigestAlgorithms() throws IOException {
055: Object o = _seq.readObject();
056:
057: if (o instanceof ASN1Set) {
058: return ((ASN1Set) o).parser();
059: }
060:
061: return (ASN1SetParser) o;
062: }
063:
064: public ContentInfoParser getEncapContentInfo() throws IOException {
065: return new ContentInfoParser((ASN1SequenceParser) _seq
066: .readObject());
067: }
068:
069: public ASN1SetParser getCertificates() throws IOException {
070: _certsCalled = true;
071: _nextObject = _seq.readObject();
072:
073: if (_nextObject instanceof ASN1TaggedObjectParser
074: && ((ASN1TaggedObjectParser) _nextObject).getTagNo() == 0) {
075: ASN1SetParser certs = (ASN1SetParser) ((ASN1TaggedObjectParser) _nextObject)
076: .getObjectParser(DERTags.SET, false);
077: _nextObject = null;
078:
079: return certs;
080: }
081:
082: return null;
083: }
084:
085: public ASN1SetParser getCrls() throws IOException {
086: if (!_certsCalled) {
087: throw new IOException("getCerts() has not been called.");
088: }
089:
090: _crlsCalled = true;
091:
092: if (_nextObject == null) {
093: _nextObject = _seq.readObject();
094: }
095:
096: if (_nextObject instanceof ASN1TaggedObjectParser
097: && ((ASN1TaggedObjectParser) _nextObject).getTagNo() == 1) {
098: ASN1SetParser crls = (ASN1SetParser) ((ASN1TaggedObjectParser) _nextObject)
099: .getObjectParser(DERTags.SET, false);
100: _nextObject = null;
101:
102: return crls;
103: }
104:
105: return null;
106: }
107:
108: public ASN1SetParser getSignerInfos() throws IOException {
109: if (!_certsCalled || !_crlsCalled) {
110: throw new IOException(
111: "getCerts() and/or getCrls() has not been called.");
112: }
113:
114: if (_nextObject == null) {
115: _nextObject = _seq.readObject();
116: }
117:
118: return (ASN1SetParser) _nextObject;
119: }
120: }
|