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.ASN1OctetString;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
011import org.apache.commons.ssl.org.bouncycastle.asn1.x509.AlgorithmIdentifier;
012
013/**
014 * <a href="http://tools.ietf.org/html/rfc5652#section-6.2.1">RFC 5652</a>:
015 * Content encryption key delivery mechanisms.
016 * <pre>
017 * KeyTransRecipientInfo ::= SEQUENCE {
018 *     version CMSVersion,  -- always set to 0 or 2
019 *     rid RecipientIdentifier,
020 *     keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
021 *     encryptedKey EncryptedKey 
022 * }
023 * </pre>
024 */
025public class KeyTransRecipientInfo
026    extends ASN1Object
027{
028    private ASN1Integer          version;
029    private RecipientIdentifier rid;
030    private AlgorithmIdentifier keyEncryptionAlgorithm;
031    private ASN1OctetString     encryptedKey;
032
033    public KeyTransRecipientInfo(
034        RecipientIdentifier rid,
035        AlgorithmIdentifier keyEncryptionAlgorithm,
036        ASN1OctetString     encryptedKey)
037    {
038        if (rid.toASN1Primitive() instanceof ASN1TaggedObject)
039        {
040            this.version = new ASN1Integer(2);
041        }
042        else
043        {
044            this.version = new ASN1Integer(0);
045        }
046
047        this.rid = rid;
048        this.keyEncryptionAlgorithm = keyEncryptionAlgorithm;
049        this.encryptedKey = encryptedKey;
050    }
051
052    /**
053     * @deprecated use getInstance()
054     */
055    public KeyTransRecipientInfo(
056        ASN1Sequence seq)
057    {
058        this.version = (ASN1Integer)seq.getObjectAt(0);
059        this.rid = RecipientIdentifier.getInstance(seq.getObjectAt(1));
060        this.keyEncryptionAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(2));
061        this.encryptedKey = (ASN1OctetString)seq.getObjectAt(3);
062    }
063
064    /**
065     * Return a KeyTransRecipientInfo object from the given object.
066     * <p>
067     * Accepted inputs:
068     * <ul>
069     * <li> null &rarr; null
070     * <li> {@link KeyTransRecipientInfo} object
071     * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats with KeyTransRecipientInfo structure inside
072     * </ul>
073     *
074     * @param obj the object we want converted.
075     * @exception IllegalArgumentException if the object cannot be converted.
076     */
077    public static KeyTransRecipientInfo getInstance(
078        Object obj)
079    {
080        if (obj instanceof KeyTransRecipientInfo)
081        {
082            return (KeyTransRecipientInfo)obj;
083        }
084        
085        if(obj != null)
086        {
087            return new KeyTransRecipientInfo(ASN1Sequence.getInstance(obj));
088        }
089        
090        return null;
091    } 
092
093    public ASN1Integer getVersion()
094    {
095        return version;
096    }
097
098    public RecipientIdentifier getRecipientIdentifier()
099    {
100        return rid;
101    }
102
103    public AlgorithmIdentifier getKeyEncryptionAlgorithm()
104    {
105        return keyEncryptionAlgorithm;
106    }
107
108    public ASN1OctetString getEncryptedKey()
109    {
110        return encryptedKey;
111    }
112
113    /** 
114     * Produce an object suitable for an ASN1OutputStream.
115     */
116    public ASN1Primitive toASN1Primitive()
117    {
118        ASN1EncodableVector  v = new ASN1EncodableVector();
119
120        v.add(version);
121        v.add(rid);
122        v.add(keyEncryptionAlgorithm);
123        v.add(encryptedKey);
124
125        return new DERSequence(v);
126    }
127}