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.ASN1Set;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
010import org.apache.commons.ssl.org.bouncycastle.asn1.BERSequence;
011import org.apache.commons.ssl.org.bouncycastle.asn1.BERTaggedObject;
012
013/**
014 * <a href="http://tools.ietf.org/html/rfc5652#section-8">RFC 5652</a> EncryptedData object.
015 * <p>
016 * <pre>
017 * EncryptedData ::= SEQUENCE {
018 *     version CMSVersion,
019 *     encryptedContentInfo EncryptedContentInfo,
020 *     unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL }
021 * </pre>
022 */
023public class EncryptedData
024    extends ASN1Object
025{
026    private ASN1Integer version;
027    private EncryptedContentInfo encryptedContentInfo;
028    private ASN1Set unprotectedAttrs;
029
030    /**
031     * Return an EncryptedData object from the given object.
032     * <p>
033     * Accepted inputs:
034     * <ul>
035     * <li> null &rarr; null
036     * <li> {@link EncryptedData} object
037     * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats
038     * </ul>
039     *
040     * @param o the object we want converted.
041     * @exception IllegalArgumentException if the object cannot be converted.
042     */
043    public static EncryptedData getInstance(Object o)
044    {
045        if (o instanceof EncryptedData)
046        {
047            return (EncryptedData)o;
048        }
049
050        if (o != null)
051        {
052            return new EncryptedData(ASN1Sequence.getInstance(o));
053        }
054
055        return null;
056    }
057
058    public EncryptedData(EncryptedContentInfo encInfo)
059    {
060        this(encInfo,  null);
061    }
062
063    public EncryptedData(EncryptedContentInfo encInfo, ASN1Set unprotectedAttrs)
064    {
065        this.version = new ASN1Integer((unprotectedAttrs == null) ? 0 : 2);
066        this.encryptedContentInfo = encInfo;
067        this.unprotectedAttrs = unprotectedAttrs;
068    }
069
070    private EncryptedData(ASN1Sequence seq)
071    {
072        this.version = ASN1Integer.getInstance(seq.getObjectAt(0));
073        this.encryptedContentInfo = EncryptedContentInfo.getInstance(seq.getObjectAt(1));
074
075        if (seq.size() == 3)
076        {
077            this.unprotectedAttrs = ASN1Set.getInstance((ASN1TaggedObject)seq.getObjectAt(2), false);
078        }
079    }
080
081    public ASN1Integer getVersion()
082    {
083        return version;
084    }
085
086    public EncryptedContentInfo getEncryptedContentInfo()
087    {
088        return encryptedContentInfo;
089    }
090
091    public ASN1Set getUnprotectedAttrs()
092    {
093        return unprotectedAttrs;
094    }
095
096    /**
097     * @return a basic ASN.1 object representation.
098     */
099    public ASN1Primitive toASN1Primitive()
100    {
101        ASN1EncodableVector v = new ASN1EncodableVector();
102
103        v.add(version);
104        v.add(encryptedContentInfo);
105        if (unprotectedAttrs != null)
106        {
107            v.add(new BERTaggedObject(false, 1, unprotectedAttrs));
108        }
109
110        return new BERSequence(v);
111    }
112}