001: package org.bouncycastle.asn1.cms;
002:
003: import org.bouncycastle.asn1.ASN1SequenceParser;
004: import org.bouncycastle.asn1.ASN1SetParser;
005: import org.bouncycastle.asn1.ASN1TaggedObjectParser;
006: import org.bouncycastle.asn1.DEREncodable;
007: import org.bouncycastle.asn1.DERInteger;
008: import org.bouncycastle.asn1.DERTags;
009:
010: import java.io.IOException;
011:
012: /**
013: * <pre>
014: * EnvelopedData ::= SEQUENCE {
015: * version CMSVersion,
016: * originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
017: * recipientInfos RecipientInfos,
018: * encryptedContentInfo EncryptedContentInfo,
019: * unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL
020: * }
021: * </pre>
022: */
023: public class EnvelopedDataParser {
024: private ASN1SequenceParser _seq;
025: private DERInteger _version;
026: private DEREncodable _nextObject;
027: private boolean _originatorInfoCalled;
028:
029: public EnvelopedDataParser(ASN1SequenceParser seq)
030: throws IOException {
031: this ._seq = seq;
032: this ._version = (DERInteger) seq.readObject();
033: }
034:
035: public DERInteger getVersion() {
036: return _version;
037: }
038:
039: public ASN1SetParser getCertificates() throws IOException {
040: if (_nextObject == null) {
041: _nextObject = _seq.readObject();
042: }
043:
044: if (_nextObject instanceof ASN1TaggedObjectParser
045: && ((ASN1TaggedObjectParser) _nextObject).getTagNo() == 0) {
046: ASN1SetParser certs = (ASN1SetParser) ((ASN1TaggedObjectParser) _nextObject)
047: .getObjectParser(DERTags.SET, false);
048: _nextObject = null;
049:
050: return certs;
051: }
052:
053: return null;
054: }
055:
056: public ASN1SetParser getCrls() throws IOException {
057: if (_nextObject == null) {
058: _nextObject = _seq.readObject();
059: }
060:
061: if (_nextObject instanceof ASN1TaggedObjectParser
062: && ((ASN1TaggedObjectParser) _nextObject).getTagNo() == 1) {
063: ASN1SetParser crls = (ASN1SetParser) ((ASN1TaggedObjectParser) _nextObject)
064: .getObjectParser(DERTags.SET, false);
065: _nextObject = null;
066:
067: return crls;
068: }
069:
070: return null;
071: }
072:
073: public OriginatorInfo getOriginatorInfo() throws IOException {
074: _originatorInfoCalled = true;
075:
076: if (_nextObject == null) {
077: _nextObject = _seq.readObject();
078: }
079:
080: if (_nextObject instanceof ASN1TaggedObjectParser
081: && ((ASN1TaggedObjectParser) _nextObject).getTagNo() == 0) {
082: ASN1SequenceParser originatorInfo = (ASN1SequenceParser) ((ASN1TaggedObjectParser) _nextObject)
083: .getObjectParser(DERTags.SEQUENCE, false);
084: _nextObject = null;
085: return OriginatorInfo.getInstance(originatorInfo
086: .getDERObject());
087: }
088:
089: return null;
090: }
091:
092: public ASN1SetParser getRecipientInfos() throws IOException {
093: if (!_originatorInfoCalled) {
094: getOriginatorInfo();
095: }
096:
097: if (_nextObject == null) {
098: _nextObject = _seq.readObject();
099: }
100:
101: ASN1SetParser recipientInfos = (ASN1SetParser) _nextObject;
102: _nextObject = null;
103: return recipientInfos;
104: }
105:
106: public EncryptedContentInfoParser getEncryptedContentInfo()
107: throws IOException {
108: if (_nextObject == null) {
109: _nextObject = _seq.readObject();
110: }
111:
112: if (_nextObject != null) {
113: ASN1SequenceParser o = (ASN1SequenceParser) _nextObject;
114: _nextObject = null;
115: return new EncryptedContentInfoParser(o);
116: }
117:
118: return null;
119: }
120:
121: public ASN1SetParser getUnprotectedAttrs() throws IOException {
122: if (_nextObject == null) {
123: _nextObject = _seq.readObject();
124: }
125:
126: if (_nextObject != null) {
127: DEREncodable o = _nextObject;
128: _nextObject = null;
129: return (ASN1SetParser) ((ASN1TaggedObjectParser) o)
130: .getObjectParser(DERTags.SET, false);
131: }
132:
133: return null;
134: }
135: }
|