001package org.apache.commons.ssl.org.bouncycastle.asn1.cms;
002
003import java.io.IOException;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1SequenceParser;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1SetParser;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObjectParser;
010import org.apache.commons.ssl.org.bouncycastle.asn1.BERTags;
011
012/** 
013 * Parser of <a href="http://tools.ietf.org/html/rfc5652#section-6.1">RFC 5652</a> {@link EnvelopedData} object.
014 * <p>
015 * <pre>
016 * EnvelopedData ::= SEQUENCE {
017 *     version CMSVersion,
018 *     originatorInfo [0] IMPLICIT OriginatorInfo OPTIONAL,
019 *     recipientInfos RecipientInfos,
020 *     encryptedContentInfo EncryptedContentInfo,
021 *     unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL 
022 * }
023 * </pre>
024 */
025public class EnvelopedDataParser
026{
027    private ASN1SequenceParser _seq;
028    private ASN1Integer        _version;
029    private ASN1Encodable      _nextObject;
030    private boolean            _originatorInfoCalled;
031    
032    public EnvelopedDataParser(
033        ASN1SequenceParser seq)
034        throws IOException
035    {
036        this._seq = seq;
037        this._version = ASN1Integer.getInstance(seq.readObject());
038    }
039
040    public ASN1Integer getVersion()
041    {
042        return _version;
043    }
044
045    public OriginatorInfo getOriginatorInfo() 
046        throws IOException
047    {
048        _originatorInfoCalled = true; 
049        
050        if (_nextObject == null)
051        {
052            _nextObject = _seq.readObject();
053        }
054        
055        if (_nextObject instanceof ASN1TaggedObjectParser && ((ASN1TaggedObjectParser)_nextObject).getTagNo() == 0)
056        {
057            ASN1SequenceParser originatorInfo = (ASN1SequenceParser) ((ASN1TaggedObjectParser)_nextObject).getObjectParser(BERTags.SEQUENCE, false);
058            _nextObject = null;
059            return OriginatorInfo.getInstance(originatorInfo.toASN1Primitive());
060        }
061        
062        return null;
063    }
064    
065    public ASN1SetParser getRecipientInfos()
066        throws IOException
067    {
068        if (!_originatorInfoCalled)
069        {
070            getOriginatorInfo();
071        }
072        
073        if (_nextObject == null)
074        {
075            _nextObject = _seq.readObject();
076        }
077        
078        ASN1SetParser recipientInfos = (ASN1SetParser)_nextObject;
079        _nextObject = null;
080        return recipientInfos;
081    }
082
083    public EncryptedContentInfoParser getEncryptedContentInfo() 
084        throws IOException
085    {
086        if (_nextObject == null)
087        {
088            _nextObject = _seq.readObject();
089        }
090        
091        
092        if (_nextObject != null)
093        {
094            ASN1SequenceParser o = (ASN1SequenceParser) _nextObject;
095            _nextObject = null;
096            return new EncryptedContentInfoParser(o);
097        }
098        
099        return null;
100    }
101
102    public ASN1SetParser getUnprotectedAttrs()
103        throws IOException
104    {
105        if (_nextObject == null)
106        {
107            _nextObject = _seq.readObject();
108        }
109        
110        
111        if (_nextObject != null)
112        {
113            ASN1Encodable o = _nextObject;
114            _nextObject = null;
115            return (ASN1SetParser)((ASN1TaggedObjectParser)o).getObjectParser(BERTags.SET, false);
116        }
117        
118        return null;
119    }
120}