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.ASN1Primitive;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
008import org.apache.commons.ssl.org.bouncycastle.asn1.DERBitString;
009import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.x509.AlgorithmIdentifier;
011
012/**
013 * <a href="http://tools.ietf.org/html/rfc5652#section-6.2.2">RFC 5652</a>:
014 * Content encryption key delivery mechanisms.
015 * <p>
016 * <pre>
017 * OriginatorPublicKey ::= SEQUENCE {
018 *     algorithm AlgorithmIdentifier,
019 *     publicKey BIT STRING 
020 * }
021 * </pre>
022 */
023public class OriginatorPublicKey
024    extends ASN1Object
025{
026    private AlgorithmIdentifier algorithm;
027    private DERBitString        publicKey;
028    
029    public OriginatorPublicKey(
030        AlgorithmIdentifier algorithm,
031        byte[]              publicKey)
032    {
033        this.algorithm = algorithm;
034        this.publicKey = new DERBitString(publicKey);
035    }
036
037    /**
038     * @deprecated use getInstance()
039     */
040    public OriginatorPublicKey(
041        ASN1Sequence seq)
042    {
043        algorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(0));
044        publicKey = (DERBitString)seq.getObjectAt(1);
045    }
046    
047    /**
048     * Return an OriginatorPublicKey object from a tagged object.
049     *
050     * @param obj the tagged object holding the object we want.
051     * @param explicit 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 OriginatorPublicKey getInstance(
057        ASN1TaggedObject    obj,
058        boolean             explicit)
059    {
060        return getInstance(ASN1Sequence.getInstance(obj, explicit));
061    }
062    
063    /**
064     * Return an OriginatorPublicKey object from the given object.
065     * <p>
066     * Accepted inputs:
067     * <ul>
068     * <li> null &rarr; null
069     * <li> {@link OriginatorPublicKey} object
070     * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats with OriginatorPublicKey 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 OriginatorPublicKey getInstance(
077        Object obj)
078    {
079        if (obj instanceof OriginatorPublicKey)
080        {
081            return (OriginatorPublicKey)obj;
082        }
083        
084        if (obj != null)
085        {
086            return new OriginatorPublicKey(ASN1Sequence.getInstance(obj));
087        }
088
089        return null;
090    } 
091
092    public AlgorithmIdentifier getAlgorithm()
093    {
094        return algorithm;
095    }
096
097    public DERBitString getPublicKey()
098    {
099        return publicKey;
100    }
101
102    /** 
103     * Produce an object suitable for an ASN1OutputStream.
104     */
105    public ASN1Primitive toASN1Primitive()
106    {
107        ASN1EncodableVector  v = new ASN1EncodableVector();
108
109        v.add(algorithm);
110        v.add(publicKey);
111
112        return new DERSequence(v);
113    }
114}