001: package org.bouncycastle.asn1.isismtt.x509;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1EncodableVector;
005: import org.bouncycastle.asn1.ASN1Sequence;
006: import org.bouncycastle.asn1.ASN1TaggedObject;
007: import org.bouncycastle.asn1.DEREncodable;
008: import org.bouncycastle.asn1.DERIA5String;
009: import org.bouncycastle.asn1.DERObject;
010: import org.bouncycastle.asn1.DERObjectIdentifier;
011: import org.bouncycastle.asn1.DERSequence;
012: import org.bouncycastle.asn1.DERString;
013: import org.bouncycastle.asn1.isismtt.ISISMTTObjectIdentifiers;
014: import org.bouncycastle.asn1.x500.DirectoryString;
015:
016: import java.util.Enumeration;
017:
018: /**
019: * Names of authorities which are responsible for the administration of title
020: * registers.
021: *
022: * <pre>
023: * NamingAuthority ::= SEQUENCE
024: * {
025: * namingAuthorityId OBJECT IDENTIFIER OPTIONAL,
026: * namingAuthorityUrl IA5String OPTIONAL,
027: * namingAuthorityText DirectoryString(SIZE(1..128)) OPTIONAL
028: * }
029: * </pre>
030: * @see org.bouncycastle.asn1.isismtt.x509.AdmissionSyntax
031: *
032: */
033: public class NamingAuthority extends ASN1Encodable {
034:
035: /**
036: * Profession OIDs should always be defined under the OID branch of the
037: * responsible naming authority. At the time of this writing, the work group
038: * �Recht, Wirtschaft, Steuern� (�Law, Economy, Taxes�) is registered as the
039: * first naming authority under the OID id-isismtt-at-namingAuthorities.
040: */
041: public static final DERObjectIdentifier id_isismtt_at_namingAuthorities_RechtWirtschaftSteuern = new DERObjectIdentifier(
042: ISISMTTObjectIdentifiers.id_isismtt_at_namingAuthorities
043: + ".1");
044:
045: private DERObjectIdentifier namingAuthorityId;
046: private String namingAuthorityUrl;
047: private DirectoryString namingAuthorityText;
048:
049: public static NamingAuthority getInstance(Object obj) {
050: if (obj == null || obj instanceof NamingAuthority) {
051: return (NamingAuthority) obj;
052: }
053:
054: if (obj instanceof ASN1Sequence) {
055: return new NamingAuthority((ASN1Sequence) obj);
056: }
057:
058: throw new IllegalArgumentException(
059: "illegal object in getInstance: "
060: + obj.getClass().getName());
061: }
062:
063: public static NamingAuthority getInstance(ASN1TaggedObject obj,
064: boolean explicit) {
065: return getInstance(ASN1Sequence.getInstance(obj, explicit));
066: }
067:
068: /**
069: * Constructor from ASN1Sequence.
070: * <p/>
071: * <p/>
072: * <pre>
073: * NamingAuthority ::= SEQUENCE
074: * {
075: * namingAuthorityId OBJECT IDENTIFIER OPTIONAL,
076: * namingAuthorityUrl IA5String OPTIONAL,
077: * namingAuthorityText DirectoryString(SIZE(1..128)) OPTIONAL
078: * }
079: * </pre>
080: *
081: * @param seq The ASN.1 sequence.
082: */
083: private NamingAuthority(ASN1Sequence seq) {
084:
085: if (seq.size() > 3) {
086: throw new IllegalArgumentException("Bad sequence size: "
087: + seq.size());
088: }
089:
090: Enumeration e = seq.getObjects();
091:
092: if (e.hasMoreElements()) {
093: DEREncodable o = (DEREncodable) e.nextElement();
094: if (o instanceof DERObjectIdentifier) {
095: namingAuthorityId = (DERObjectIdentifier) o;
096: } else if (o instanceof DERIA5String) {
097: namingAuthorityUrl = DERIA5String.getInstance(o)
098: .getString();
099: } else if (o instanceof DERString) {
100: namingAuthorityText = DirectoryString.getInstance(o);
101: } else {
102: throw new IllegalArgumentException(
103: "Bad object encountered: " + o.getClass());
104: }
105: }
106: if (e.hasMoreElements()) {
107: DEREncodable o = (DEREncodable) e.nextElement();
108: if (o instanceof DERIA5String) {
109: namingAuthorityUrl = DERIA5String.getInstance(o)
110: .getString();
111: } else if (o instanceof DERString) {
112: namingAuthorityText = DirectoryString.getInstance(o);
113: } else {
114: throw new IllegalArgumentException(
115: "Bad object encountered: " + o.getClass());
116: }
117: }
118: if (e.hasMoreElements()) {
119: DEREncodable o = (DEREncodable) e.nextElement();
120: if (o instanceof DERString) {
121: namingAuthorityText = DirectoryString.getInstance(o);
122: } else {
123: throw new IllegalArgumentException(
124: "Bad object encountered: " + o.getClass());
125: }
126:
127: }
128: }
129:
130: /**
131: * @return Returns the namingAuthorityId.
132: */
133: public DERObjectIdentifier getNamingAuthorityId() {
134: return namingAuthorityId;
135: }
136:
137: /**
138: * @return Returns the namingAuthorityText.
139: */
140: public DirectoryString getNamingAuthorityText() {
141: return namingAuthorityText;
142: }
143:
144: /**
145: * @return Returns the namingAuthorityUrl.
146: */
147: public String getNamingAuthorityUrl() {
148: return namingAuthorityUrl;
149: }
150:
151: /**
152: * Constructor from given details.
153: * <p/>
154: * All parameters can be combined.
155: *
156: * @param namingAuthorityId ObjectIdentifier for naming authority.
157: * @param namingAuthorityUrl URL for naming authority.
158: * @param namingAuthorityText Textual representation of naming authority.
159: */
160: public NamingAuthority(DERObjectIdentifier namingAuthorityId,
161: String namingAuthorityUrl,
162: DirectoryString namingAuthorityText) {
163: this .namingAuthorityId = namingAuthorityId;
164: this .namingAuthorityUrl = namingAuthorityUrl;
165: this .namingAuthorityText = namingAuthorityText;
166: }
167:
168: /**
169: * Produce an object suitable for an ASN1OutputStream.
170: * <p/>
171: * Returns:
172: * <p/>
173: * <pre>
174: * NamingAuthority ::= SEQUENCE
175: * {
176: * namingAuthorityId OBJECT IDENTIFIER OPTIONAL,
177: * namingAuthorityUrl IA5String OPTIONAL,
178: * namingAuthorityText DirectoryString(SIZE(1..128)) OPTIONAL
179: * }
180: * </pre>
181: *
182: * @return a DERObject
183: */
184: public DERObject toASN1Object() {
185: ASN1EncodableVector vec = new ASN1EncodableVector();
186: if (namingAuthorityId != null) {
187: vec.add(namingAuthorityId);
188: }
189: if (namingAuthorityUrl != null) {
190: vec.add(new DERIA5String(namingAuthorityUrl, true));
191: }
192: if (namingAuthorityText != null) {
193: vec.add(namingAuthorityText);
194: }
195: return new DERSequence(vec);
196: }
197: }
|