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.ASN1Object;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1OctetString;
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.DERSequence;
010
011/**
012 * <a href="http://tools.ietf.org/html/rfc5652#section-6.2.2">RFC 5652</a>:
013 * Content encryption key delivery mechanisms.
014 * <pre>
015 * RecipientEncryptedKey ::= SEQUENCE {
016 *     rid KeyAgreeRecipientIdentifier,
017 *     encryptedKey EncryptedKey
018 * }
019 * </pre>
020 */
021public class RecipientEncryptedKey
022    extends ASN1Object
023{
024    private KeyAgreeRecipientIdentifier identifier;
025    private ASN1OctetString encryptedKey;
026
027    private RecipientEncryptedKey(
028        ASN1Sequence seq)
029    {
030        identifier = KeyAgreeRecipientIdentifier.getInstance(seq.getObjectAt(0));
031        encryptedKey = (ASN1OctetString)seq.getObjectAt(1);
032    }
033    
034    /**
035     * Return an RecipientEncryptedKey object from a tagged object.
036     *
037     * @param obj the tagged object holding the object we want.
038     * @param explicit true if the object is meant to be explicitly
039     *              tagged false otherwise.
040     * @exception IllegalArgumentException if the object held by the
041     *          tagged object cannot be converted.
042     */
043    public static RecipientEncryptedKey getInstance(
044        ASN1TaggedObject    obj,
045        boolean             explicit)
046    {
047        return getInstance(ASN1Sequence.getInstance(obj, explicit));
048    }
049    
050    /**
051     * Return a RecipientEncryptedKey object from the given object.
052     * <p>
053     * Accepted inputs:
054     * <ul>
055     * <li> null &rarr; null
056     * <li> {@link RecipientEncryptedKey} object
057     * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats with RecipientEncryptedKey structure inside
058     * </ul>
059     *
060     * @param obj the object we want converted.
061     * @exception IllegalArgumentException if the object cannot be converted.
062     */
063    public static RecipientEncryptedKey getInstance(
064        Object obj)
065    {
066        if (obj instanceof RecipientEncryptedKey)
067        {
068            return (RecipientEncryptedKey)obj;
069        }
070        
071        if (obj != null)
072        {
073            return new RecipientEncryptedKey(ASN1Sequence.getInstance(obj));
074        }
075        
076        return null;
077    } 
078
079    public RecipientEncryptedKey(
080        KeyAgreeRecipientIdentifier id,
081        ASN1OctetString             encryptedKey)
082    {
083        this.identifier = id;
084        this.encryptedKey = encryptedKey;
085    }
086
087    public KeyAgreeRecipientIdentifier getIdentifier()
088    {
089        return identifier;
090    }
091
092    public ASN1OctetString getEncryptedKey()
093    {
094        return encryptedKey;
095    }
096
097    /** 
098     * Produce an object suitable for an ASN1OutputStream.
099     */
100    public ASN1Primitive toASN1Primitive()
101    {
102        ASN1EncodableVector  v = new ASN1EncodableVector();
103
104        v.add(identifier);
105        v.add(encryptedKey);
106
107        return new DERSequence(v);
108    }
109}