001package org.apache.commons.ssl.org.bouncycastle.asn1.cms;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Encodable;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
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.DERSequence;
010
011/**
012 * <a href="http://tools.ietf.org/html/rfc5652#section-10.2.7">RFC 5652</a>: OtherKeyAttribute object.
013 * <p>
014 * <pre>
015 * OtherKeyAttribute ::= SEQUENCE {
016 *     keyAttrId OBJECT IDENTIFIER,
017 *     keyAttr ANY DEFINED BY keyAttrId OPTIONAL
018 * }
019 * </pre>
020 */
021public class OtherKeyAttribute
022    extends ASN1Object
023{
024    private ASN1ObjectIdentifier keyAttrId;
025    private ASN1Encodable        keyAttr;
026
027    /**
028     * Return an OtherKeyAttribute object from the given object.
029     * <p>
030     * Accepted inputs:
031     * <ul>
032     * <li> null &rarr; null
033     * <li> {@link OtherKeyAttribute} object
034     * <li> {@link org.bouncycastle.asn1.ASN1Sequence#getInstance(java.lang.Object) ASN1Sequence} input formats with OtherKeyAttribute structure inside
035     * </ul>
036     *
037     * @param o the object we want converted.
038     * @exception IllegalArgumentException if the object cannot be converted.
039     */
040    public static OtherKeyAttribute getInstance(
041        Object o)
042    {
043        if (o instanceof OtherKeyAttribute)
044        {
045            return (OtherKeyAttribute)o;
046        }
047        
048        if (o != null)
049        {
050            return new OtherKeyAttribute(ASN1Sequence.getInstance(o));
051        }
052
053        return null;
054    }
055
056    /**
057     * @deprecated use getInstance()
058     */
059    public OtherKeyAttribute(
060        ASN1Sequence seq)
061    {
062        keyAttrId = (ASN1ObjectIdentifier)seq.getObjectAt(0);
063        keyAttr = seq.getObjectAt(1);
064    }
065
066    public OtherKeyAttribute(
067        ASN1ObjectIdentifier keyAttrId,
068        ASN1Encodable        keyAttr)
069    {
070        this.keyAttrId = keyAttrId;
071        this.keyAttr = keyAttr;
072    }
073
074    public ASN1ObjectIdentifier getKeyAttrId()
075    {
076        return keyAttrId;
077    }
078    
079    public ASN1Encodable getKeyAttr()
080    {
081        return keyAttr;
082    }
083
084    /** 
085     * Produce an object suitable for an ASN1OutputStream.
086     */
087    public ASN1Primitive toASN1Primitive()
088    {
089        ASN1EncodableVector v = new ASN1EncodableVector();
090
091        v.add(keyAttrId);
092        v.add(keyAttr);
093
094        return new DERSequence(v);
095    }
096}