FileDocCategorySizeDatePackage
CMSEnvelopedDataParser.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)8651Wed Oct 01 10:55:30 BST 2008org.bouncycastle.cms

CMSEnvelopedDataParser

public class CMSEnvelopedDataParser extends CMSContentInfoParser
Parsing class for an CMS Enveloped Data object from an input stream.

Note: that because we are in a streaming mode only one recipient can be tried and it is important that the methods on the parser are called in the appropriate order.

Example of use - assuming the first recipient matches the private key we have.

CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(inputStream);

RecipientInformationStore recipients = ep.getRecipientInfos();

Collection c = recipients.getRecipients();
Iterator it = c.iterator();

if (it.hasNext())
{
RecipientInformation recipient = (RecipientInformation)it.next();

CMSTypedStream recData = recipient.getContentStream(privateKey, "BC");

processDataStream(recData.getContentStream());
}
Note: this class does not introduce buffering - if you are processing large files you should create the parser with:
CMSEnvelopedDataParser ep = new CMSEnvelopedDataParser(new BufferedInputStream(inputStream, bufSize));
where bufSize is a suitably large buffer size.

Fields Summary
RecipientInformationStore
_recipientInfoStore
org.bouncycastle.asn1.cms.EnvelopedDataParser
_envelopedData
private org.bouncycastle.asn1.x509.AlgorithmIdentifier
_encAlg
private org.bouncycastle.asn1.cms.AttributeTable
_unprotectedAttributes
private boolean
_attrNotRead
Constructors Summary
public CMSEnvelopedDataParser(byte[] envelopedData)

        this(new ByteArrayInputStream(envelopedData));
    
public CMSEnvelopedDataParser(InputStream envelopedData)

        super(envelopedData);

        this._attrNotRead = true;
        this._envelopedData = new EnvelopedDataParser((ASN1SequenceParser)_contentInfo.getContent(DERTags.SEQUENCE));

        // TODO Validate version?
		//DERInteger version = this._envelopedData.getVersion();

        //
        // load the RecipientInfoStore
        //
        ASN1SetParser s = _envelopedData.getRecipientInfos();
        List          baseInfos = new ArrayList();

        DEREncodable entry;
        while ((entry = s.readObject()) != null)
        {
            baseInfos.add(RecipientInfo.getInstance(entry.getDERObject()));
        }

        //
        // read the encrypted content info
        //
        EncryptedContentInfoParser encInfo = _envelopedData.getEncryptedContentInfo();
        
        this._encAlg = encInfo.getContentEncryptionAlgorithm();
        
        //
        // prime the recipients
        //
        List        infos = new ArrayList();
        Iterator    it = baseInfos.iterator();
        InputStream dataStream = ((ASN1OctetStringParser)encInfo.getEncryptedContent(DERTags.OCTET_STRING)).getOctetStream();
        
        while (it.hasNext())
        {
            RecipientInfo   info = (RecipientInfo)it.next();
            DEREncodable    recipInfo = info.getInfo();    

            if (recipInfo instanceof KeyTransRecipientInfo)
            {
                infos.add(new KeyTransRecipientInformation(
                            (KeyTransRecipientInfo)recipInfo, _encAlg, dataStream));
            }
            else if (recipInfo instanceof KEKRecipientInfo)
            {
                infos.add(new KEKRecipientInformation(
                            (KEKRecipientInfo)recipInfo, _encAlg, dataStream));
            }
            else if (recipInfo instanceof KeyAgreeRecipientInfo)
            {
                infos.add(new KeyAgreeRecipientInformation(
                            (KeyAgreeRecipientInfo)recipInfo, _encAlg, dataStream));
            }
            else if (recipInfo instanceof PasswordRecipientInfo)
            {
                infos.add(new PasswordRecipientInformation(
                            (PasswordRecipientInfo)recipInfo, _encAlg, dataStream));
            }
        }
        
        _recipientInfoStore = new RecipientInformationStore(infos);
    
Methods Summary
private byte[]encodeObj(org.bouncycastle.asn1.DEREncodable obj)

        if (obj != null)
        {
            return obj.getDERObject().getEncoded();
        }

        return null;
    
public java.lang.StringgetEncryptionAlgOID()
return the object identifier for the content encryption algorithm.

        return _encAlg.getObjectId().toString();
    
public byte[]getEncryptionAlgParams()
return the ASN.1 encoded encryption algorithm parameters, or null if there aren't any.

        try
        {
            return encodeObj(_encAlg.getParameters());
        }
        catch (Exception e)
        {
            throw new RuntimeException("exception getting encryption parameters " + e);
        }
    
public java.security.AlgorithmParametersgetEncryptionAlgorithmParameters(java.lang.String provider)
Return an AlgorithmParameters object giving the encryption parameters used to encrypt the message content.

param
provider the name of the provider to generate the parameters for.
return
the parameters object, null if there is not one.
throws
CMSException if the algorithm cannot be found, or the parameters can't be parsed.
throws
NoSuchProviderException if the provider cannot be found.

        return getEncryptionAlgorithmParameters(CMSUtils.getProvider(provider));
    
public java.security.AlgorithmParametersgetEncryptionAlgorithmParameters(java.security.Provider provider)
Return an AlgorithmParameters object giving the encryption parameters used to encrypt the message content.

param
provider the provider to generate the parameters for.
return
the parameters object, null if there is not one.
throws
CMSException if the algorithm cannot be found, or the parameters can't be parsed.

        return CMSEnvelopedHelper.INSTANCE.getEncryptionAlgorithmParameters(getEncryptionAlgOID(), getEncryptionAlgParams(), provider);
    
public RecipientInformationStoregetRecipientInfos()
return a store of the intended recipients for this message

        return _recipientInfoStore;
    
public org.bouncycastle.asn1.cms.AttributeTablegetUnprotectedAttributes()
return a table of the unprotected attributes indexed by the OID of the attribute.

exception
IOException

        if (_unprotectedAttributes == null && _attrNotRead)
        {
            ASN1SetParser             set = _envelopedData.getUnprotectedAttrs();
            
            _attrNotRead = false;
            
            if (set != null)
            {
                ASN1EncodableVector v = new ASN1EncodableVector();
                DEREncodable        o;
                
                while ((o = set.readObject()) != null)
                {
                    ASN1SequenceParser    seq = (ASN1SequenceParser)o;
                    
                    v.add(seq.getDERObject());
                }
                
                _unprotectedAttributes = new AttributeTable(new DERSet(v));
            }
        }

        return _unprotectedAttributes;