1package org.bouncycastle.asn1.x509;
2
3import org.bouncycastle.asn1.ASN1Encodable;
4import org.bouncycastle.asn1.ASN1EncodableVector;
5import org.bouncycastle.asn1.ASN1ObjectIdentifier;
6import org.bouncycastle.asn1.ASN1Sequence;
7import org.bouncycastle.asn1.ASN1Set;
8import org.bouncycastle.asn1.DERObject;
9import org.bouncycastle.asn1.DERObjectIdentifier;
10import org.bouncycastle.asn1.DERSequence;
11
12public class Attribute
13    extends ASN1Encodable
14{
15    private DERObjectIdentifier attrType;
16    private ASN1Set             attrValues;
17
18    /**
19     * return an Attribute object from the given object.
20     *
21     * @param o the object we want converted.
22     * @exception IllegalArgumentException if the object cannot be converted.
23     */
24    public static Attribute getInstance(
25        Object o)
26    {
27        if (o == null || o instanceof Attribute)
28        {
29            return (Attribute)o;
30        }
31
32        if (o instanceof ASN1Sequence)
33        {
34            return new Attribute((ASN1Sequence)o);
35        }
36
37        throw new IllegalArgumentException("unknown object in factory: " + o.getClass().getName());
38    }
39
40    public Attribute(
41        ASN1Sequence seq)
42    {
43        if (seq.size() != 2)
44        {
45            throw new IllegalArgumentException("Bad sequence size: " + seq.size());
46        }
47
48        attrType = DERObjectIdentifier.getInstance(seq.getObjectAt(0));
49        attrValues = ASN1Set.getInstance(seq.getObjectAt(1));
50    }
51
52    public Attribute(
53        DERObjectIdentifier attrType,
54        ASN1Set             attrValues)
55    {
56        this.attrType = attrType;
57        this.attrValues = attrValues;
58    }
59
60    public ASN1ObjectIdentifier getAttrType()
61    {
62        return new ASN1ObjectIdentifier(attrType.getId());
63    }
64
65    public ASN1Encodable[] getAttributeValues()
66    {
67        return attrValues.toArray();
68    }
69
70    public ASN1Set getAttrValues()
71    {
72        return attrValues;
73    }
74
75    /**
76     * Produce an object suitable for an ASN1OutputStream.
77     * <pre>
78     * Attribute ::= SEQUENCE {
79     *     attrType OBJECT IDENTIFIER,
80     *     attrValues SET OF AttributeValue
81     * }
82     * </pre>
83     */
84    public DERObject toASN1Object()
85    {
86        ASN1EncodableVector v = new ASN1EncodableVector();
87
88        v.add(attrType);
89        v.add(attrValues);
90
91        return new DERSequence(v);
92    }
93}
94