01: package org.bouncycastle.asn1.x509;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1Sequence;
06: import org.bouncycastle.asn1.ASN1Set;
07: import org.bouncycastle.asn1.DERObject;
08: import org.bouncycastle.asn1.DERObjectIdentifier;
09: import org.bouncycastle.asn1.DERSequence;
10:
11: public class Attribute extends ASN1Encodable {
12: private DERObjectIdentifier attrType;
13: private ASN1Set attrValues;
14:
15: /**
16: * return an Attribute object from the given object.
17: *
18: * @param o the object we want converted.
19: * @exception IllegalArgumentException if the object cannot be converted.
20: */
21: public static Attribute getInstance(Object o) {
22: if (o == null || o instanceof Attribute) {
23: return (Attribute) o;
24: }
25:
26: if (o instanceof ASN1Sequence) {
27: return new Attribute((ASN1Sequence) o);
28: }
29:
30: throw new IllegalArgumentException("unknown object in factory");
31: }
32:
33: public Attribute(ASN1Sequence seq) {
34: if (seq.size() != 2) {
35: throw new IllegalArgumentException("Bad sequence size: "
36: + seq.size());
37: }
38:
39: attrType = DERObjectIdentifier.getInstance(seq.getObjectAt(0));
40: attrValues = ASN1Set.getInstance(seq.getObjectAt(1));
41: }
42:
43: public Attribute(DERObjectIdentifier attrType, ASN1Set attrValues) {
44: this .attrType = attrType;
45: this .attrValues = attrValues;
46: }
47:
48: public DERObjectIdentifier getAttrType() {
49: return attrType;
50: }
51:
52: public ASN1Set getAttrValues() {
53: return attrValues;
54: }
55:
56: /**
57: * Produce an object suitable for an ASN1OutputStream.
58: * <pre>
59: * Attribute ::= SEQUENCE {
60: * attrType OBJECT IDENTIFIER,
61: * attrValues SET OF AttributeValue
62: * }
63: * </pre>
64: */
65: public DERObject toASN1Object() {
66: ASN1EncodableVector v = new ASN1EncodableVector();
67:
68: v.add(attrType);
69: v.add(attrValues);
70:
71: return new DERSequence(v);
72: }
73: }
|