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