KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > GeneralName


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1.x509;
19
20 import org.apache.geronimo.util.asn1.ASN1Choice;
21 import org.apache.geronimo.util.asn1.ASN1Encodable;
22 import org.apache.geronimo.util.asn1.ASN1OctetString;
23 import org.apache.geronimo.util.asn1.ASN1Sequence;
24 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
25 import org.apache.geronimo.util.asn1.DEREncodable;
26 import org.apache.geronimo.util.asn1.DERIA5String;
27 import org.apache.geronimo.util.asn1.DERObject;
28 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
29 import org.apache.geronimo.util.asn1.DERTaggedObject;
30
31 /**
32  * The GeneralName object.
33  * <pre>
34  * GeneralName ::= CHOICE {
35  * otherName [0] OtherName,
36  * rfc822Name [1] IA5String,
37  * dNSName [2] IA5String,
38  * x400Address [3] ORAddress,
39  * directoryName [4] Name,
40  * ediPartyName [5] EDIPartyName,
41  * uniformResourceIdentifier [6] IA5String,
42  * iPAddress [7] OCTET STRING,
43  * registeredID [8] OBJECT IDENTIFIER}
44  *
45  * OtherName ::= SEQUENCE {
46  * type-id OBJECT IDENTIFIER,
47  * value [0] EXPLICIT ANY DEFINED BY type-id }
48  *
49  * EDIPartyName ::= SEQUENCE {
50  * nameAssigner [0] DirectoryString OPTIONAL,
51  * partyName [1] DirectoryString }
52  *
53  * Name ::= CHOICE { RDNSequence }
54  * </pre>
55  */

56 public class GeneralName
57     extends ASN1Encodable
58     implements ASN1Choice
59 {
60     public static final int otherName = 0;
61     public static final int rfc822Name = 1;
62     public static final int dNSName = 2;
63     public static final int x400Address = 3;
64     public static final int directoryName = 4;
65     public static final int ediPartyName = 5;
66     public static final int uniformResourceIdentifier = 6;
67     public static final int iPAddress = 7;
68     public static final int registeredID = 8;
69
70     DEREncodable obj;
71     int tag;
72
73     public GeneralName(
74         X509Name dirName)
75     {
76         this.obj = dirName;
77         this.tag = 4;
78     }
79
80     /**
81      * @deprecated this constructor seems the wrong way round! Use GeneralName(tag, name).
82      */

83     public GeneralName(
84         DERObject name, int tag)
85     {
86         this.obj = name;
87         this.tag = tag;
88     }
89
90     /**
91      * When the subjectAltName extension contains an Internet mail address,
92      * the address MUST be included as an rfc822Name. The format of an
93      * rfc822Name is an "addr-spec" as defined in RFC 822 [RFC 822].
94      *
95      * When the subjectAltName extension contains a domain name service
96      * label, the domain name MUST be stored in the dNSName (an IA5String).
97      * The name MUST be in the "preferred name syntax," as specified by RFC
98      * 1034 [RFC 1034].
99      *
100      * When the subjectAltName extension contains a URI, the name MUST be
101      * stored in the uniformResourceIdentifier (an IA5String). The name MUST
102      * be a non-relative URL, and MUST follow the URL syntax and encoding
103      * rules specified in [RFC 1738]. The name must include both a scheme
104      * (e.g., "http" or "ftp") and a scheme-specific-part. The scheme-
105      * specific-part must include a fully qualified domain name or IP
106      * address as the host.
107      *
108      * When the subjectAltName extension contains a iPAddress, the address
109      * MUST be stored in the octet string in "network byte order," as
110      * specified in RFC 791 [RFC 791]. The least significant bit (LSB) of
111      * each octet is the LSB of the corresponding byte in the network
112      * address. For IP Version 4, as specified in RFC 791, the octet string
113      * MUST contain exactly four octets. For IP Version 6, as specified in
114      * RFC 1883, the octet string MUST contain exactly sixteen octets [RFC
115      * 1883].
116      */

117     public GeneralName(
118         int tag,
119         ASN1Encodable name)
120     {
121         this.obj = name;
122         this.tag = tag;
123     }
124
125     /**
126      * Create a General name for the given tag from the passed in String.
127      *
128      * @param tag tag number
129      * @param name string representation of name
130      */

131     public GeneralName(
132         int tag,
133         String JavaDoc name)
134     {
135         if (tag == rfc822Name || tag == dNSName || tag == uniformResourceIdentifier)
136         {
137             this.tag = tag;
138             this.obj = new DERIA5String(name);
139         }
140         else if (tag == registeredID)
141         {
142             this.tag = tag;
143             this.obj = new DERObjectIdentifier(name);
144         }
145         else
146         {
147             throw new IllegalArgumentException JavaDoc("can't process String for tag: " + tag);
148         }
149     }
150
151     public static GeneralName getInstance(
152         Object JavaDoc obj)
153     {
154         if (obj == null || obj instanceof GeneralName)
155         {
156             return (GeneralName)obj;
157         }
158
159         if (obj instanceof ASN1TaggedObject)
160         {
161             ASN1TaggedObject tagObj = (ASN1TaggedObject)obj;
162             int tag = tagObj.getTagNo();
163
164             switch (tag)
165             {
166             case otherName:
167                 return new GeneralName(ASN1Sequence.getInstance(tagObj, false), tag);
168             case rfc822Name:
169                 return new GeneralName(DERIA5String.getInstance(tagObj, false), tag);
170             case dNSName:
171                 return new GeneralName(DERIA5String.getInstance(tagObj, false), tag);
172             case x400Address:
173                 throw new IllegalArgumentException JavaDoc("unknown tag: " + tag);
174             case directoryName:
175                 return new GeneralName(ASN1Sequence.getInstance(tagObj, true), tag);
176             case ediPartyName:
177                 return new GeneralName(ASN1Sequence.getInstance(tagObj, false), tag);
178             case uniformResourceIdentifier:
179                 return new GeneralName(DERIA5String.getInstance(tagObj, false), tag);
180             case iPAddress:
181                 return new GeneralName(ASN1OctetString.getInstance(tagObj, false), tag);
182             case registeredID:
183                 return new GeneralName(DERObjectIdentifier.getInstance(tagObj, false), tag);
184             }
185         }
186
187         throw new IllegalArgumentException JavaDoc("unknown object in getInstance");
188     }
189
190     public static GeneralName getInstance(
191         ASN1TaggedObject tagObj,
192         boolean explicit)
193     {
194         return GeneralName.getInstance(ASN1TaggedObject.getInstance(tagObj, true));
195     }
196
197     public int getTagNo()
198     {
199         return tag;
200     }
201
202     public DEREncodable getName()
203     {
204         return obj;
205     }
206
207     public DERObject toASN1Object()
208     {
209         if (tag == directoryName) // directoryName is explicitly tagged as it is a CHOICE
210
{
211             return new DERTaggedObject(true, tag, obj);
212         }
213         else
214         {
215             return new DERTaggedObject(false, tag, obj);
216         }
217     }
218 }
219
Popular Tags