summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/cert/X500NameSubsystem.java
blob: de5e233c90a24eb8245f9121f6bc672615c8c760 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// --- 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 com.netscape.cmscore.cert;

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

import netscape.security.util.DerValue;
import netscape.security.util.ObjectIdentifier;
import netscape.security.x509.AVAValueConverter;
import netscape.security.x509.DirStrConverter;
import netscape.security.x509.X500NameAttrMap;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.ISubsystem;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.cmscore.util.Debug;

/**
 * Subsystem for configuring X500Name related things.
 * It is used for the following.
 * <ul>
 * <li>Add X500Name (string to oid) maps for attributes that are not supported by default.
 * <li>Specify an order for encoding Directory Strings other than the default.
 * </ul>
 * 
 * @author lhsiao
 * @version $Revision$
 */
public class X500NameSubsystem implements ISubsystem {

    private IConfigStore mConfig = null;
    public static final String ID = "X500Name";
    private String mId = ID;

    private static final String PROP_DIR_STR_ENCODING_ORDER = "directoryStringEncodingOrder";

    private static final String PROP_ATTR = "attr";
    private static final String PROP_OID = "oid";
    private static final String PROP_CLASS = "class";

    private X500NameSubsystem() {
    }

    /**
     * Retrieves subsystem identifier.
     */
    public String getId() {
        return mId;
    }

    public void setId(String id) throws EBaseException {
        mId = id;
    }

    // singleton enforcement

    private static X500NameSubsystem mInstance = new X500NameSubsystem();

    public static X500NameSubsystem getInstance() {
        return mInstance;
    }

    /**
     * Initializes this subsystem with the given configuration store.
     * All paramters are optional.
     * <ul>
     * <li>Change encoding order of Directory Strings:
     * 
     * <pre>
     * X500Name.directoryStringEncodingOrder=order seperated by commas
     * For example: Printable,BMPString,UniversalString.
     * </pre>
     * 
     * Possible values are:
     * <ul>
     * <li>Printable
     * <li>IA5String
     * <li>UniversalString
     * <li>BMPString
     * <li>UTF8String
     * </ul>
     * <p>
     * <li>Add X500Name attributes:
     * 
     * <pre>
     * X500Name.attr.attribute-name.oid=n.n.n.n
     * X500Name.attr.attribute-name.class=value converter class
     * </pre>
     * 
     * The value converter class converts a string to a ASN.1 value. It must implement
     * netscape.security.x509.AVAValueConverter interface. Converter classes provided in CMS are:
     * 
     * <pre>
     *     netscape.security.x509.PrintableConverter - 
     * 		Converts to a Printable String value. String must have only 
     * 		printable characters. 
     *     netscape.security.x509.IA5StringConverter - 
     * 		Converts to a IA5String value. String must have only IA5String
     * 		characters. 
     *     netscape.security.x509.DirStrConverter - 
     * 		Converts to a Directory (v3) String. String is expected to 
     * 		be in Directory String format according to rfc2253.
     *     netscape.security.x509.GenericValueConverter - 
     * 		Converts string character by character in the following order
     * 		from smaller character sets to broadest character set.
     * 			Printable, IA5String, BMPString, Universal String.
     * </pre>
     * 
     * </ul>
     * <P>
     * 
     * @param owner owner of this subsystem
     * @param config configuration store
     */
    public synchronized void init(ISubsystem owner, IConfigStore config)
            throws EBaseException {
        mLogger = CMS.getLogger();
        if (Debug.ON) {
            Debug.trace(ID + " started");
        }
        mConfig = config;

        // get order for encoding directory strings if any.
        setDirStrEncodingOrder();

        // load x500 name maps 
        loadX500NameAttrMaps();
    }

    /**
     * Loads X500Name String to attribute maps.
     * Called from init.
     */
    private void loadX500NameAttrMaps()
            throws EBaseException {
        X500NameAttrMap globalMap = X500NameAttrMap.getDefault();
        IConfigStore attrSubStore = mConfig.getSubStore(PROP_ATTR);
        Enumeration<String> attrNames = attrSubStore.getSubStoreNames();

        while (attrNames.hasMoreElements()) {
            String name = (String) attrNames.nextElement();
            IConfigStore substore = attrSubStore.getSubStore(name);
            String oidString = substore.getString(PROP_OID);
            ObjectIdentifier oid = CertUtils.checkOID(name, oidString);
            String className = substore.getString(PROP_CLASS);

            AVAValueConverter convClass = null;

            try {
                convClass = (AVAValueConverter)
                        Class.forName(className).newInstance();
            } catch (Exception e) {
                throw new EBaseException(
                        CMS.getUserMessage("CMS_BASE_LOAD_CLASS_FAILED", className, e.toString()));
            }
            globalMap.addNameOID(name, oid, convClass);
            if (Debug.ON) {
                Debug.trace(ID + ": Loaded " + name + " " + oid + " " + className);
            }
        }
    }

    /**
     * Set directory string encoding order.
     * Called from init().
     */
    private void setDirStrEncodingOrder()
            throws EBaseException {
        String order = mConfig.getString(PROP_DIR_STR_ENCODING_ORDER, null);

        if (order == null || order.length() == 0) // nothing.
            return;
        StringTokenizer toker = new StringTokenizer(order, ", \t");
        int numTokens = toker.countTokens();

        if (numTokens == 0) {
            String msg = "must be a list of DER tag names seperated by commas.";

            log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_CERT_DIR_STRING", PROP_DIR_STR_ENCODING_ORDER));
            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE",
                        PROP_DIR_STR_ENCODING_ORDER, msg));
        }

        byte[] tags = new byte[numTokens];

        for (int i = 0; toker.hasMoreTokens(); i++) {
            String nextTag = (String) toker.nextToken();

            try {
                tags[i] = derStr2Tag(nextTag);
            } catch (IllegalArgumentException e) {
                String msg = "unknown DER tag '" + nextTag + "'.";

                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSCORE_CERT_UNKNOWN_TAG", PROP_DIR_STR_ENCODING_ORDER, nextTag));
                throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTR_VALUE",
                            PROP_DIR_STR_ENCODING_ORDER, msg));
            }
        }

        DirStrConverter.setDefEncodingOrder(tags);
    }

    private static String PRINTABLESTRING = "PrintableString";
    private static String IA5STRING = "IA5String";
    private static String VISIBLESTRING = "VisibleString";
    private static String T61STRING = "T61String";
    private static String BMPSTRING = "BMPString";
    private static String UNIVERSALSTRING = "UniversalString";
    private static String UFT8STRING = "UTF8String";
    private static Hashtable<String, Byte> mDerStr2TagHash = new Hashtable<String, Byte>();

    static {
        mDerStr2TagHash.put(
                PRINTABLESTRING, Byte.valueOf(DerValue.tag_PrintableString));
        mDerStr2TagHash.put(
                IA5STRING, Byte.valueOf(DerValue.tag_IA5String));
        mDerStr2TagHash.put(
                VISIBLESTRING, Byte.valueOf(DerValue.tag_VisibleString));
        mDerStr2TagHash.put(
                T61STRING, Byte.valueOf(DerValue.tag_T61String));
        mDerStr2TagHash.put(
                BMPSTRING, Byte.valueOf(DerValue.tag_BMPString));
        mDerStr2TagHash.put(
                UNIVERSALSTRING, Byte.valueOf(DerValue.tag_UniversalString));
        mDerStr2TagHash.put(
                UFT8STRING, Byte.valueOf(DerValue.tag_UTF8String));
    }

    private byte derStr2Tag(String s) {
        if (s == null || s.length() == 0)
            throw new IllegalArgumentException();
        Byte tag = mDerStr2TagHash.get(s);

        if (tag == null)
            throw new IllegalArgumentException();
        return tag.byteValue();
    }

    public void startup() throws EBaseException {
    }

    /**
     * Stops this system.
     */
    public synchronized void shutdown() {
    }

    /*
     * Returns the root configuration storage of this system.
     * <P>
     *
     * @return configuration store of this subsystem
     */
    public IConfigStore getConfigStore() {
        return mConfig;
    }

    protected ILogger mLogger = null;

    protected void log(int level, String msg) {
        mLogger.log(ILogger.EV_SYSTEM,
                ILogger.S_ADMIN, level, msg);
    }

}