summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/netscape/security/util/ASN1CharStrConvMap.java
blob: da0fd45c5b0fac1ea42189e4c58fd54594ab8559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package netscape.security.util;

import java.util.Enumeration;
import java.util.Hashtable;

import sun.io.ByteToCharConverter;
import sun.io.ByteToCharUTF8;
import sun.io.CharToByteConverter;
import sun.io.CharToByteUTF8;

/**
 * Maps a ASN.1 character string type to a CharToByte and ByteToChar converter.
 * The converter is used to convert a DerValue of a ASN.1 character string type
 * from bytes to unicode characters and vice versa.
 *
 * <p>A global default ASN1CharStrConvMap is created when the class is
 * initialized. The global default map is extensible.
 *
 * @author Lily Hsiao
 * @author Slava Galperin
 *
 */

public class ASN1CharStrConvMap
{
    // public constructors

    /**
     * Constructs a ASN1CharStrConvMap.
     */
    public ASN1CharStrConvMap()
    {
    }

    /**
     * Get a Character to Byte converter for the specified DER tag.
     *
     * @param tag 	A DER tag of a ASN.1 character string type,
     *			for example DerValue.tag_PrintableString.
     *
     * @return 		A CharToByteConverter for the DER tag.
     *
     * @exception InstantiationException
     *		if error occurs when instantiating the CharToByteConverter.
     * @exception IllegalAccessException
     *		if error occurs when loading the CharToByteConverter class.
     */
    public CharToByteConverter getCBC(byte tag)
	throws IllegalAccessException, InstantiationException
    {
	Byte tagObj = Byte.valueOf(tag);
	CharToByteConverter cbc = null;
	Class<CharToByteConverter> cbcClass;
	cbcClass = (Class<CharToByteConverter>)tag2CBC.get(tagObj);
	if (cbcClass == null)
	    return null;
	cbc = (CharToByteConverter)cbcClass.newInstance();
	cbc.setSubstitutionMode(false);
	return cbc;
    }

    /**
     * Get a Byte to Character converter for the given DER tag.
     *
     * @param tag 	A DER tag of a ASN.1 character string type,
     *			for example DerValue.tag_PrintableString.
     *
     * @return 		A ByteToCharConverter for the DER tag.
     *
     * @exception InstantiationException
     *		if error occurs when instantiationg the ByteToCharConverter.
     * @exception IllegalAccessException
     *		if error occurs when loading the ByteToCharConverter class.
     */
    public ByteToCharConverter getBCC(byte tag)
	throws IllegalAccessException, InstantiationException
    {
	Byte tagObj = Byte.valueOf(tag);
	ByteToCharConverter bcc = null;
	Class<ByteToCharConverter> bccClass = tag2BCC.get(tagObj);
	if (bccClass == null)
	    return null;
	bcc = (ByteToCharConverter)bccClass.newInstance();
	bcc.setSubstitutionMode(false);
	return bcc;
    }

    /**
     * Add a tag-CharToByteConverter-ByteToCharConverter entry in the map.
     *
     * @param tag	A DER tag of a ASN.1 character string type,
     *			ex. DerValue.tag_IA5String
     * @param cbc	A CharToByteConverter for the tag.
     * @param bcc	A ByteToCharConverter for the tag.
     */
	@SuppressWarnings("unchecked")
	public void addEntry(byte tag, Class<?> cbc, Class<?> bcc)
    {
	Class<CharToByteConverter> current_cbc;
	Class<ByteToCharConverter> current_bcc;
	Byte tagByte = Byte.valueOf(tag);

	current_cbc = (Class<CharToByteConverter>)tag2CBC.get(tagByte);
	current_bcc = (Class<ByteToCharConverter>)tag2BCC.get(tagByte);
	if (current_cbc != null || current_bcc != null)
	{
	    if (current_cbc != cbc || current_bcc != bcc)
	    {
		throw new IllegalArgumentException(
		    "a DER tag to converter entry already exists.");
	    }
	    else {
		return;
	    }
	}
	if (!CharToByteConverter.class.isAssignableFrom(cbc) ||
	    !ByteToCharConverter.class.isAssignableFrom(bcc)) {
	    throw new IllegalArgumentException(
		"arguments not a CharToByteConverter or ByteToCharConverter");
	}
	tag2CBC.put(tagByte, (Class<CharToByteConverter>) cbc);
	tag2BCC.put(tagByte, (Class<ByteToCharConverter>) bcc);
    }

    /**
     * Get and enumeration of all tags in the map.
     * @return 	An Enumeration of DER tags in the map as Bytes.
     */
    public Enumeration<Byte> getTags()
    {
	return tag2CBC.keys();
    }

    // static public methods.

    /**
     * Get the global ASN1CharStrConvMap.
     * @return 	The global default ASN1CharStrConvMap.
     */
    static public ASN1CharStrConvMap getDefault()
    {
	return defaultMap;
    }

    /**
     * Set the global default ASN1CharStrConvMap.
     * @param newDefault 	The new default ASN1CharStrConvMap.
     */
    static public void setDefault(ASN1CharStrConvMap newDefault)
    {
	if (newDefault == null)
	    throw new IllegalArgumentException(
	       "Cannot set a null default Der Tag Converter map");
	defaultMap = newDefault;
    }

    // private methods and variables.

    private Hashtable<Byte, Class<CharToByteConverter>> tag2CBC = new Hashtable<Byte, Class<CharToByteConverter>>();
    private Hashtable<Byte, Class<ByteToCharConverter>> tag2BCC = new Hashtable<Byte, Class<ByteToCharConverter>>();

    private static ASN1CharStrConvMap defaultMap;

    /**
     * Create the default converter map on initialization
     */
    static {
	defaultMap = new ASN1CharStrConvMap();
	defaultMap.addEntry(DerValue.tag_PrintableString,
	    	(Class<?>)CharToBytePrintable.class, (Class<?>)ByteToCharPrintable.class);
	defaultMap.addEntry(DerValue.tag_VisibleString,
	    	CharToBytePrintable.class, ByteToCharPrintable.class);
	defaultMap.addEntry(DerValue.tag_IA5String,
	    	CharToByteIA5String.class, ByteToCharIA5String.class);
	defaultMap.addEntry(DerValue.tag_BMPString,
	        // Changed by bskim
	    	//sun.io.CharToByteUnicode.class,
	    	//netscape.security.util.ByteToCharUnicode.class);
	    	sun.io.CharToByteUnicodeBig.class,
	    	sun.io.ByteToCharUnicodeBig.class);
	    	// Change end
	defaultMap.addEntry(DerValue.tag_UniversalString,
	    	CharToByteUniversalString.class,
	    	ByteToCharUniversalString.class);
    	// XXX this is an oversimplified implementation of T.61 strings, it
    	// doesn't handle all cases
	defaultMap.addEntry(DerValue.tag_T61String,
	    	latin1CBC.class, latin1BCC.class);
	// UTF8String added to ASN.1 in 1998
	defaultMap.addEntry(DerValue.tag_UTF8String,
	    	CharToByteUTF8.class,
	    	ByteToCharUTF8.class);
	defaultMap.addEntry(DerValue.tag_GeneralString,
	    	CharToByteUTF8.class,
	    	ByteToCharUTF8.class);
    };

};

class latin1CBC extends sun.io.CharToByteISO8859_1 {
	public latin1CBC() {
		super();
		subMode = false;
	}
}

class latin1BCC extends sun.io.ByteToCharISO8859_1 {
	public latin1BCC() {
		super();
		subMode = false;
	}
}