summaryrefslogtreecommitdiffstats
path: root/base/util/src/netscape/security/x509/OIDMap.java
blob: 41c1fa28927b6775c0832a01a3b31695e1ee9dce (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// --- 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.x509;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

import netscape.security.util.ObjectIdentifier;

/**
 * This class defines the mapping from OID & name to classes and vice
 * versa. Used by CertificateExtensions & PKCS10 to get the java
 * classes associated with a particular OID/name.
 *
 * @author Amit Kapoor
 * @author Hemma Prafullchandra
 * @version 1.12
 */
public class OIDMap {

    /**
     * Location for where the OID/Classes maps are stored on
     * the local system.
     */
    public static final String EXTENSIONS_HOME =
            (System.getProperty("java.home") + File.separator + "lib"
                    + File.separator + "security" + File.separator + "cert"
            + File.separator);
    /**
     * File names for where OIDs and Classes are registered
     * for V3 extensions.
     */
    public static final String EXTENSIONS_OIDS = "x509extensions.oid";
    public static final String EXTENSIONS_CLASSES = "x509extensions.classes";

    // Make default names easier
    private static final String ROOT = X509CertImpl.NAME + "." +
                                 X509CertInfo.NAME + "." +
                                 X509CertInfo.EXTENSIONS;
    private static final String AUTH_KEY_IDENTIFIER = ROOT + "." +
                                          AuthorityKeyIdentifierExtension.NAME;
    private static final String SUB_KEY_IDENTIFIER = ROOT + "." +
                                          SubjectKeyIdentifierExtension.NAME;
    private static final String KEY_USAGE = ROOT + "." +
                                          KeyUsageExtension.NAME;
    private static final String PRIVATE_KEY_USAGE = ROOT + "." +
                                          PrivateKeyUsageExtension.NAME;
    private static final String POLICY_MAPPINGS = ROOT + "." +
                                          PolicyMappingsExtension.NAME;
    private static final String SUB_ALT_NAME = ROOT + "." +
                                          SubjectAlternativeNameExtension.NAME;
    private static final String ISSUER_ALT_NAME = ROOT + "." +
                                          IssuerAlternativeNameExtension.NAME;
    private static final String BASIC_CONSTRAINTS = ROOT + "." +
                                          BasicConstraintsExtension.NAME;
    private static final String NAME_CONSTRAINTS = ROOT + "." +
                                          NameConstraintsExtension.NAME;
    private static final String POLICY_CONSTRAINTS = ROOT + "." +
                                          PolicyConstraintsExtension.NAME;
    private static final String CERT_POLICIES = //ROOT + "." +
            CertificatePoliciesExtension.NAME;
    private static final String SUBJ_DIR_ATTR = //ROOT + "." +
            SubjectDirAttributesExtension.NAME;
    public static final String EXT_KEY_USAGE_NAME = "ExtendedKeyUsageExtension";
    public static final String EXT_INHIBIT_ANY_POLICY_NAME = "InhibitAnyPolicyExtension";
    private static final String EXT_KEY_USAGE = //ROOT + "." +
            EXT_KEY_USAGE_NAME;

    private static final String CRL_NUMBER = ROOT + "." +
                                          CRLNumberExtension.NAME;
    private static final String CRL_REASON = ROOT + "." +
                                          CRLReasonExtension.NAME;

    private static final Hashtable<ObjectIdentifier, String> oid2Name = new Hashtable<ObjectIdentifier, String>();
    private static final Hashtable<String, ObjectIdentifier> name2OID = new Hashtable<String, ObjectIdentifier>();
    private static final Hashtable<String, String> name2Class = new Hashtable<String, String>();

    // Initialize recognized extensions from EXTENSIONS_{OIDS/CLASSES} files
    static {
        loadNames();
        loadClasses();
    }

    // Load the default name to oid map (EXTENSIONS_OIDS)
    private static void loadNamesDefault(Properties props) {
        props.put(SUB_KEY_IDENTIFIER, "2.5.29.14");
        props.put(KEY_USAGE, "2.5.29.15");
        props.put(PRIVATE_KEY_USAGE, "2.5.29.16");
        props.put(SUB_ALT_NAME, "2.5.29.17");
        props.put(ISSUER_ALT_NAME, "2.5.29.18");
        props.put(BASIC_CONSTRAINTS, "2.5.29.19");
        props.put(CRL_NUMBER, "2.5.29.20");
        props.put(CRL_REASON, "2.5.29.21");
        props.put(NAME_CONSTRAINTS, "2.5.29.30");
        props.put(POLICY_MAPPINGS, "2.5.29.33");
        props.put(POLICY_CONSTRAINTS, "2.5.29.36");
        props.put(CERT_POLICIES, "2.5.29.32");
        props.put(AUTH_KEY_IDENTIFIER, "2.5.29.35");
        props.put(SUBJ_DIR_ATTR, "2.5.29.9");
        props.put(EXT_KEY_USAGE, "2.5.29.37");
    }

    // Load the default name to class map (EXTENSIONS_CLASSES)
    private static void loadClassDefault(Properties props) {
        props.put(AUTH_KEY_IDENTIFIER,
                   "netscape.security.x509.AuthorityKeyIdentifierExtension");
        props.put(SUB_KEY_IDENTIFIER,
                  "netscape.security.x509.SubjectKeyIdentifierExtension");
        props.put(KEY_USAGE,
                  "netscape.security.x509.KeyUsageExtension");
        props.put(PRIVATE_KEY_USAGE,
                  "netscape.security.x509.PrivateKeyUsageExtension");
        props.put(POLICY_MAPPINGS,
                  "netscape.security.x509.PolicyMappingsExtension");
        props.put(SUB_ALT_NAME,
                  "netscape.security.x509.SubjectAlternativeNameExtension");
        props.put(ISSUER_ALT_NAME,
                  "netscape.security.x509.IssuerAlternativeNameExtension");
        props.put(BASIC_CONSTRAINTS,
                  "netscape.security.x509.BasicConstraintsExtension");
        props.put(NAME_CONSTRAINTS,
                  "netscape.security.x509.NameConstraintsExtension");
        props.put(POLICY_CONSTRAINTS,
                  "netscape.security.x509.PolicyConstraintsExtension");
        props.put(CERT_POLICIES,
                  "netscape.security.x509.CertificatePoliciesExtension");
        props.put(SUBJ_DIR_ATTR,
                  "netscape.security.x509.SubjectDirAttributesExtension");
        props.put(EXT_KEY_USAGE,
                  "netscape.security.extensions.ExtendedKeyUsageExtension");
        props.put(CRL_NUMBER, "netscape.security.x509.CRLNumberExtension");
        props.put(CRL_REASON, "netscape.security.x509.CRLReasonExtension");
    }

    // Return the file along with location
    private static File certificatePropFile(String fileName) {
        return (new File(EXTENSIONS_HOME + fileName));
    }

    // Load the names to oid map
    private static void loadNames() {
        Properties props = new Properties();
        File namesMap = certificatePropFile(EXTENSIONS_OIDS);

        if (!namesMap.exists()) {
            loadNamesDefault(props);
        } else {
            try {
                FileInputStream fis = new FileInputStream(namesMap);
                props.load(fis);
                fis.close();
            } catch (IOException e) {
                loadNamesDefault(props);
            }
        }

        Iterator<String> names = props.stringPropertyNames().iterator();
        while (names.hasNext()) {
            String name = names.next();
            String oidName = props.getProperty(name);
            ObjectIdentifier oid = new ObjectIdentifier(oidName);

            name2OID.put(name, oid);
            oid2Name.put(oid, name);
        }
    }

    // Load the names to classes map
    private static void loadClasses() {
        Properties props = new Properties();
        File classMap = certificatePropFile(EXTENSIONS_CLASSES);

        if (!classMap.exists()) {
            loadClassDefault(props);
        } else {
            try {
                FileInputStream fis = new FileInputStream(classMap);
                props.load(fis);
            } catch (IOException e) {
                loadClassDefault(props);
            }
        }

        Iterator<String> names = props.stringPropertyNames().iterator();
        while (names.hasNext()) {
            String name = names.next();
            String className = props.getProperty(name);

            name2Class.put(name, className);
        }
    }

    /**
     * Add a name to lookup table.
     *
     * @param className the name of the fully qualified class implementing
     *            the asn object.
     * @param oid the string representation of the object identifier for
     *            the class.
     * @param name the name of the attribute.
     * @exception CertificateException on errors.
     */
    public static void addAttribute(String className, String oid, String name)
            throws CertificateException {
        ObjectIdentifier objId = new ObjectIdentifier(oid);
        if (oid2Name.get(objId) != null) {
            throw new CertificateException("Object identifier already exists.");
        }
        if (name2OID.get(name) != null) {
            throw new CertificateException("Name already exists.");
        }
        if (name2Class.get(className) != null) {
            throw new CertificateException("Class already exists.");
        }
        oid2Name.put(objId, name);
        name2OID.put(name, objId);
        name2Class.put(name, className);
    }

    /**
     * Return user friendly name associated with the OID.
     *
     * @param oid the name of the object identifier to be returned.
     * @return the user friendly name or null if no name
     *         is registered for this oid.
     */
    public static String getName(ObjectIdentifier oid) {
        return (String) oid2Name.get(oid);
    }

    /**
     * Return Object identifier for user friendly name.
     *
     * @param name the user friendly name.
     * @return the Object Identifier or null if no oid
     *         is registered for this name.
     */
    public static ObjectIdentifier getOID(String name) {
        return (ObjectIdentifier) name2OID.get(name);
    }

    /**
     * Return the java class object associated with the user friendly name.
     *
     * @param name the user friendly name.
     * @exception CertificateException if class cannot be instantiated.
     */
    public static Class<?> getClass(String name) throws CertificateException {
        String className = (String) name2Class.get(name);
        if (className == null)
            return null;
        try {
            Class<?> extClass = Class.forName(className);
            return (extClass);
        } catch (Exception e) {
            throw new CertificateException("Error instantiating class for "
                                + name + " " + e.toString());
        }
    }

    /**
     * Return the java class object associated with the object identifier..
     *
     * @param oid the name of the object identifier to be returned.
     * @exception CertificateException if class cannot be instatiated.
     */
    public static Class<?> getClass(ObjectIdentifier oid)
            throws CertificateException {
        String name = getName(oid);
        if (name == null)
            return null;
        String className = (String) name2Class.get(name);
        if (className == null)
            return null;
        try {
            Class<?> extClass = Class.forName(className);
            return (extClass);
        } catch (Exception e) {
            throw new CertificateException("Error instantiating class for "
                                   + name + " " + e.toString());
        }
    }
}