001package org.apache.commons.ssl.org.bouncycastle.asn1.cms;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Integer;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
009import org.apache.commons.ssl.org.bouncycastle.asn1.BERSequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.x509.AlgorithmIdentifier;
011
012/** 
013 * <a href="http://tools.ietf.org/html/rfc3274">RFC 3274</a>: CMS Compressed Data.
014 * 
015 * <pre>
016 * CompressedData ::= SEQUENCE {
017 *     version CMSVersion,
018 *     compressionAlgorithm CompressionAlgorithmIdentifier,
019 *     encapContentInfo EncapsulatedContentInfo
020 * }
021 * </pre>
022 */
023public class CompressedData
024    extends ASN1Object
025{
026    private ASN1Integer           version;
027    private AlgorithmIdentifier  compressionAlgorithm;
028    private ContentInfo          encapContentInfo;
029
030    public CompressedData(
031        AlgorithmIdentifier compressionAlgorithm,
032        ContentInfo         encapContentInfo)
033    {
034        this.version = new ASN1Integer(0);
035        this.compressionAlgorithm = compressionAlgorithm;
036        this.encapContentInfo = encapContentInfo;
037    }
038    
039    private CompressedData(
040        ASN1Sequence seq)
041    {
042        this.version = (ASN1Integer)seq.getObjectAt(0);
043        this.compressionAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(1));
044        this.encapContentInfo = ContentInfo.getInstance(seq.getObjectAt(2));
045    }
046
047    /**
048     * Return a CompressedData object from a tagged object.
049     *
050     * @param ato the tagged object holding the object we want.
051     * @param isExplicit true if the object is meant to be explicitly
052     *              tagged false otherwise.
053     * @exception IllegalArgumentException if the object held by the
054     *          tagged object cannot be converted.
055     */
056    public static CompressedData getInstance(
057        ASN1TaggedObject ato,
058        boolean isExplicit)
059    {
060        return getInstance(ASN1Sequence.getInstance(ato, isExplicit));
061    }
062    
063    /**
064     * Return a CompressedData object from the given object.
065     * <p>
066     * Accepted inputs:
067     * <ul>
068     * <li> null &rarr; null
069     * <li> {@link CompressedData} object
070     * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats with CompressedData structure inside
071     * </ul>
072     *
073     * @param obj the object we want converted.
074     * @exception IllegalArgumentException if the object cannot be converted.
075     */
076    public static CompressedData getInstance(
077        Object obj)
078    {
079        if (obj instanceof CompressedData)
080        {
081            return (CompressedData)obj;
082        }
083
084        if (obj != null)
085        {
086            return new CompressedData(ASN1Sequence.getInstance(obj));
087        }
088
089        return null;
090    }
091
092    public ASN1Integer getVersion()
093    {
094        return version;
095    }
096
097    public AlgorithmIdentifier getCompressionAlgorithmIdentifier()
098    {
099        return compressionAlgorithm;
100    }
101
102    public ContentInfo getEncapContentInfo()
103    {
104        return encapContentInfo;
105    }
106
107    public ASN1Primitive toASN1Primitive()
108    {
109        ASN1EncodableVector v = new ASN1EncodableVector();
110
111        v.add(version);
112        v.add(compressionAlgorithm);
113        v.add(encapContentInfo);
114
115        return new BERSequence(v);
116    }
117}