1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Encodable;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1EncodableVector;
54c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
66e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.ASN1ObjectIdentifier;
74c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Set;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class Attribute
134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
154c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private ASN1ObjectIdentifier attrType;
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private ASN1Set             attrValues;
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Attribute object from the given object.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param o the object we want converted.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static Attribute getInstance(
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object o)
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
274c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (o instanceof Attribute)
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (Attribute)o;
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
324c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (o != null)
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
344c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new Attribute(ASN1Sequence.getInstance(o));
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
374c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return null;
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
404c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private Attribute(
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence seq)
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (seq.size() != 2)
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("Bad sequence size: " + seq.size());
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
484c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        attrType = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0));
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        attrValues = ASN1Set.getInstance(seq.getObjectAt(1));
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public Attribute(
534c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1ObjectIdentifier attrType,
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Set             attrValues)
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.attrType = attrType;
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.attrValues = attrValues;
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
606e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    public ASN1ObjectIdentifier getAttrType()
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
626e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        return new ASN1ObjectIdentifier(attrType.getId());
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
646e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
656e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    public ASN1Encodable[] getAttributeValues()
666e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    {
676e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        return attrValues.toArray();
686e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    }
696e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1Set getAttrValues()
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return attrValues;
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Produce an object suitable for an ASN1OutputStream.
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Attribute ::= SEQUENCE {
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     attrType OBJECT IDENTIFIER,
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *     attrValues SET OF AttributeValue
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * }
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
844c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector v = new ASN1EncodableVector();
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(attrType);
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(attrValues);
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERSequence(v);
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
94