001package org.apache.commons.ssl.org.bouncycastle.asn1.esf;
002
003import java.util.Enumeration;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
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.DERTaggedObject;
012import org.apache.commons.ssl.org.bouncycastle.asn1.x509.Attribute;
013import org.apache.commons.ssl.org.bouncycastle.asn1.x509.AttributeCertificate;
014
015
016public class SignerAttribute
017    extends ASN1Object
018{
019    private Object[] values;
020
021    public static SignerAttribute getInstance(
022        Object o)
023    {
024        if (o instanceof SignerAttribute)
025        {
026            return (SignerAttribute) o;
027        }
028        else if (o != null)
029        {
030            return new SignerAttribute(ASN1Sequence.getInstance(o));
031        }
032
033        return null;
034    }
035
036    private SignerAttribute(
037        ASN1Sequence seq)
038    {
039        int index = 0;
040        values = new Object[seq.size()];
041
042        for (Enumeration e = seq.getObjects(); e.hasMoreElements();)
043        {
044            ASN1TaggedObject taggedObject = ASN1TaggedObject.getInstance(e.nextElement());
045
046            if (taggedObject.getTagNo() == 0)
047            {
048                ASN1Sequence attrs = ASN1Sequence.getInstance(taggedObject, true);
049                Attribute[]  attributes = new Attribute[attrs.size()];
050
051                for (int i = 0; i != attributes.length; i++)
052                {
053                    attributes[i] = Attribute.getInstance(attrs.getObjectAt(i));
054                }
055                values[index] = attributes;
056            }
057            else if (taggedObject.getTagNo() == 1)
058            {
059                values[index] = AttributeCertificate.getInstance(ASN1Sequence.getInstance(taggedObject, true));
060            }
061            else
062            {
063                throw new IllegalArgumentException("illegal tag: " + taggedObject.getTagNo());
064            }
065            index++;
066        }
067    }
068
069    public SignerAttribute(
070        Attribute[] claimedAttributes)
071    {
072        this.values = new Object[1];
073        this.values[0] = claimedAttributes;
074    }
075
076    public SignerAttribute(
077        AttributeCertificate certifiedAttributes)
078    {
079        this.values = new Object[1];
080        this.values[0] = certifiedAttributes;
081    }
082
083    /**
084     * Return the sequence of choices - the array elements will either be of
085     * type Attribute[] or AttributeCertificate depending on what tag was used.
086     *
087     * @return array of choices.
088     */
089    public Object[] getValues()
090    {
091        return values;
092    }
093
094    /**
095     *
096     * <pre>
097     *  SignerAttribute ::= SEQUENCE OF CHOICE {
098     *      claimedAttributes   [0] ClaimedAttributes,
099     *      certifiedAttributes [1] CertifiedAttributes }
100     *
101     *  ClaimedAttributes ::= SEQUENCE OF Attribute
102     *  CertifiedAttributes ::= AttributeCertificate -- as defined in RFC 3281: see clause 4.1.
103     * </pre>
104     */
105    public ASN1Primitive toASN1Primitive()
106    {
107        ASN1EncodableVector v = new ASN1EncodableVector();
108
109        for (int i = 0; i != values.length; i++)
110        {
111            if (values[i] instanceof Attribute[])
112            {
113                v.add(new DERTaggedObject(0, new DERSequence((Attribute[])values[i])));
114            }
115            else
116            {
117                v.add(new DERTaggedObject(1, (AttributeCertificate)values[i]));
118            }
119        }
120
121        return new DERSequence(v);
122    }
123}