KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.apache.geronimo.util.asn1.ASN1InputStream;
24 import org.apache.geronimo.util.asn1.DERObject;
25 import org.apache.geronimo.util.asn1.DERObjectIdentifier;
26
27 /**
28  * It turns out that the number of standard ways the fields in a DN should be
29  * encoded into their ASN.1 counterparts is rapidly approaching the
30  * number of machines on the internet. By default the X509Name class
31  * will produce PrintableStrings if the field value will decode to that,
32  * next UTF8Strings if the field value will decode to that, and finally BMPStrings
33  * if 16 bit characters are required.
34  * <p>
35  * The way this is done is with a default encoder which is
36  * implemented as follows:
37  * <pre>
38  * public class X509DefaultEntryConverter
39  * extends X509NameEntryConverter
40  * {
41  * public DERObject getConvertedValue(
42  * DERObjectIdentifier oid,
43  * String value)
44  * {
45  * if (str.length() != 0 && str.charAt(0) == '#')
46  * {
47  * return convertHexEncoded(str, 1);
48  * }
49  * if (oid.equals(EmailAddress))
50  * {
51  * return new DERIA5String(str);
52  * }
53  * else if (canBePrintable(str))
54  * {
55  * return new DERPrintableString(str);
56  * }
57  * else if (canBeUTF8(str))
58  * {
59  * return new DERUTF8String(str);
60  * }
61  * else
62  * {
63  * return new DERBMPString(str);
64  * }
65  * }
66  * }
67  */

68 public abstract class X509NameEntryConverter
69 {
70     /**
71      * Convert an inline encoded hex string rendition of an ASN.1
72      * object back into its corresponding ASN.1 object.
73      *
74      * @param str the hex encoded object
75      * @param off the index at which the encoding starts
76      * @return the decoded object
77      */

78     protected DERObject convertHexEncoded(
79         String JavaDoc str,
80         int off)
81         throws IOException JavaDoc
82     {
83         str = str.toLowerCase();
84         byte[] data = new byte[str.length() / 2];
85         for (int index = 0; index != data.length; index++)
86         {
87             char left = str.charAt((index * 2) + off);
88             char right = str.charAt((index * 2) + off + 1);
89
90             if (left < 'a')
91             {
92                 data[index] = (byte)((left - '0') << 4);
93             }
94             else
95             {
96                 data[index] = (byte)((left - 'a' + 10) << 4);
97             }
98             if (right < 'a')
99             {
100                 data[index] |= (byte)(right - '0');
101             }
102             else
103             {
104                 data[index] |= (byte)(right - 'a' + 10);
105             }
106         }
107
108         ASN1InputStream aIn = new ASN1InputStream(
109                                             new ByteArrayInputStream JavaDoc(data));
110
111         return aIn.readObject();
112     }
113
114     /**
115      * return true if the passed in String can be represented without
116      * loss as a PrintableString, false otherwise.
117      */

118     protected boolean canBePrintable(
119         String JavaDoc str)
120     {
121         for (int i = str.length() - 1; i >= 0; i--)
122         {
123             char ch = str.charAt(i);
124
125             if (str.charAt(i) > 0x007f)
126             {
127                 return false;
128             }
129
130             if ('a' <= ch && ch <= 'z')
131             {
132                 continue;
133             }
134
135             if ('A' <= ch && ch <= 'Z')
136             {
137                 continue;
138             }
139
140             if ('0' <= ch && ch <= '9')
141             {
142                 continue;
143             }
144
145             switch (ch)
146             {
147             case ' ':
148             case '\'':
149             case '(':
150             case ')':
151             case '+':
152             case '-':
153             case '.':
154             case ':':
155             case '=':
156             case '?':
157                 continue;
158             }
159
160             return false;
161         }
162
163         return true;
164     }
165
166     /**
167      * return true if the passed in String can be represented without
168      * loss as a UTF8String, false otherwise.
169      */

170     protected boolean canBeUTF8(
171         String JavaDoc str)
172     {
173         for (int i = str.length() - 1; i >= 0; i--)
174         {
175             if (str.charAt(i) > 0x00ff)
176             {
177                 return false;
178             }
179         }
180
181         return true;
182     }
183
184     /**
185      * Convert the passed in String value into the appropriate ASN.1
186      * encoded object.
187      *
188      * @param oid the oid associated with the value in the DN.
189      * @param value the value of the particular DN component.
190      * @return the ASN.1 equivalent for the value.
191      */

192     public abstract DERObject getConvertedValue(DERObjectIdentifier oid, String JavaDoc value);
193 }
194
Popular Tags