001: package org.bouncycastle.asn1.x509;
002:
003: import java.util.Enumeration;
004: import java.util.Vector;
005:
006: import org.bouncycastle.asn1.ASN1Encodable;
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1OctetString;
009: import org.bouncycastle.asn1.ASN1Sequence;
010: import org.bouncycastle.asn1.ASN1TaggedObject;
011: import org.bouncycastle.asn1.DERObject;
012: import org.bouncycastle.asn1.DERObjectIdentifier;
013: import org.bouncycastle.asn1.DEROctetString;
014: import org.bouncycastle.asn1.DERSequence;
015: import org.bouncycastle.asn1.DERTaggedObject;
016: import org.bouncycastle.asn1.DERUTF8String;
017:
018: /**
019: * Implementation of <code>IetfAttrSyntax</code> as specified by RFC3281.
020: */
021: public class IetfAttrSyntax extends ASN1Encodable {
022: public static final int VALUE_OCTETS = 1;
023: public static final int VALUE_OID = 2;
024: public static final int VALUE_UTF8 = 3;
025: GeneralNames policyAuthority = null;
026: Vector values = new Vector();
027: int valueChoice = -1;
028:
029: /**
030: *
031: */
032: public IetfAttrSyntax(ASN1Sequence seq) {
033: int i = 0;
034:
035: if (seq.getObjectAt(0) instanceof ASN1TaggedObject) {
036: policyAuthority = GeneralNames.getInstance(
037: ((ASN1TaggedObject) seq.getObjectAt(0)), false);
038: i++;
039: } else if (seq.size() == 2) { // VOMS fix
040: policyAuthority = GeneralNames.getInstance(seq
041: .getObjectAt(0));
042: i++;
043: }
044:
045: if (!(seq.getObjectAt(i) instanceof ASN1Sequence)) {
046: throw new IllegalArgumentException(
047: "Non-IetfAttrSyntax encoding");
048: }
049:
050: seq = (ASN1Sequence) seq.getObjectAt(i);
051:
052: for (Enumeration e = seq.getObjects(); e.hasMoreElements();) {
053: DERObject obj = (DERObject) e.nextElement();
054: int type;
055:
056: if (obj instanceof DERObjectIdentifier) {
057: type = VALUE_OID;
058: } else if (obj instanceof DERUTF8String) {
059: type = VALUE_UTF8;
060: } else if (obj instanceof DEROctetString) {
061: type = VALUE_OCTETS;
062: } else {
063: throw new IllegalArgumentException(
064: "Bad value type encoding IetfAttrSyntax");
065: }
066:
067: if (valueChoice < 0) {
068: valueChoice = type;
069: }
070:
071: if (type != valueChoice) {
072: throw new IllegalArgumentException(
073: "Mix of value types in IetfAttrSyntax");
074: }
075:
076: values.addElement(obj);
077: }
078: }
079:
080: public GeneralNames getPolicyAuthority() {
081: return policyAuthority;
082: }
083:
084: public int getValueType() {
085: return valueChoice;
086: }
087:
088: public Object[] getValues() {
089: if (this .getValueType() == VALUE_OCTETS) {
090: ASN1OctetString[] tmp = new ASN1OctetString[values.size()];
091:
092: for (int i = 0; i != tmp.length; i++) {
093: tmp[i] = (ASN1OctetString) values.elementAt(i);
094: }
095:
096: return tmp;
097: } else if (this .getValueType() == VALUE_OID) {
098: DERObjectIdentifier[] tmp = new DERObjectIdentifier[values
099: .size()];
100:
101: for (int i = 0; i != tmp.length; i++) {
102: tmp[i] = (DERObjectIdentifier) values.elementAt(i);
103: }
104:
105: return tmp;
106: } else {
107: DERUTF8String[] tmp = new DERUTF8String[values.size()];
108:
109: for (int i = 0; i != tmp.length; i++) {
110: tmp[i] = (DERUTF8String) values.elementAt(i);
111: }
112:
113: return tmp;
114: }
115: }
116:
117: /**
118: *
119: * <pre>
120: *
121: * IetfAttrSyntax ::= SEQUENCE {
122: * policyAuthority [0] GeneralNames OPTIONAL,
123: * values SEQUENCE OF CHOICE {
124: * octets OCTET STRING,
125: * oid OBJECT IDENTIFIER,
126: * string UTF8String
127: * }
128: * }
129: *
130: * </pre>
131: */
132: public DERObject toASN1Object() {
133: ASN1EncodableVector v = new ASN1EncodableVector();
134:
135: if (policyAuthority != null) {
136: v.add(new DERTaggedObject(0, policyAuthority));
137: }
138:
139: ASN1EncodableVector v2 = new ASN1EncodableVector();
140:
141: for (Enumeration i = values.elements(); i.hasMoreElements();) {
142: v2.add((ASN1Encodable) i.nextElement());
143: }
144:
145: v.add(new DERSequence(v2));
146:
147: return new DERSequence(v);
148: }
149: }
|