001: /*
002: * @(#)OIDMap.java 1.4 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: /*
029: * Note that there are two versions of OIDMap, this subsetted
030: * one for CDC/FP and another for the security optional package.
031: * Be sure you're editing the right one!
032: */
033:
034: package sun.security.x509;
035:
036: import java.util.*;
037: import java.io.IOException;
038:
039: import java.security.cert.CertificateException;
040: import java.security.cert.CertificateParsingException;
041:
042: import sun.security.util.*;
043:
044: /**
045: * This class defines the mapping from OID & name to classes and vice
046: * versa. Used by CertificateExtensions & PKCS10 to get the java
047: * classes associated with a particular OID/name.
048: *
049: * @author Amit Kapoor
050: * @author Hemma Prafullchandra
051: * @author Andreas Sterbenz
052: *
053: * @version 1.4, 10/10/06
054: */
055: public class OIDMap {
056:
057: private OIDMap() {
058: // empty
059: }
060:
061: // "user-friendly" names
062: private static final String ROOT = X509CertImpl.NAME + "."
063: + X509CertInfo.NAME + "." + X509CertInfo.EXTENSIONS;
064: private static final String AUTH_KEY_IDENTIFIER = ROOT + "."
065: + AuthorityKeyIdentifierExtension.NAME;
066: private static final String SUB_KEY_IDENTIFIER = ROOT + "."
067: + SubjectKeyIdentifierExtension.NAME;
068: private static final String KEY_USAGE = ROOT + "."
069: + KeyUsageExtension.NAME;
070: private static final String PRIVATE_KEY_USAGE = ROOT + "."
071: + PrivateKeyUsageExtension.NAME;
072: private static final String POLICY_MAPPINGS = ROOT + "."
073: + PolicyMappingsExtension.NAME;
074: private static final String SUB_ALT_NAME = ROOT + "."
075: + SubjectAlternativeNameExtension.NAME;
076: private static final String ISSUER_ALT_NAME = ROOT + "."
077: + IssuerAlternativeNameExtension.NAME;
078: private static final String BASIC_CONSTRAINTS = ROOT + "."
079: + BasicConstraintsExtension.NAME;
080: private static final String NAME_CONSTRAINTS = ROOT + "."
081: + NameConstraintsExtension.NAME;
082: private static final String POLICY_CONSTRAINTS = ROOT + "."
083: + PolicyConstraintsExtension.NAME;
084: private static final String CRL_NUMBER = ROOT + "."
085: + CRLNumberExtension.NAME;
086: private static final String CRL_REASON = ROOT + "."
087: + CRLReasonCodeExtension.NAME;
088: private static final String NETSCAPE_CERT = ROOT + "."
089: + NetscapeCertTypeExtension.NAME;
090: /* CDC/FP subsets away CertificatePoliciesExtension
091: *private static final String CERT_POLICIES = ROOT + "." +
092: * CertificatePoliciesExtension.NAME;
093: */
094: private static final String EXT_KEY_USAGE = ROOT + "."
095: + ExtendedKeyUsageExtension.NAME;
096: /*
097: * CDC/FP subsets InhibitAnyPolicyExtension and
098: * CRLDistributionPointsExtension to
099: * the security optional package
100: private static final String INHIBIT_ANY_POLICY = ROOT + "." +
101: InhibitAnyPolicyExtension.NAME;
102: private static final String CRL_DIST_POINTS = ROOT + "." +
103: CRLDistributionPointsExtension.NAME;
104: */
105:
106: /** Map ObjectIdentifier(oid) -> OIDInfo(info) */
107: private final static Map oidMap;
108:
109: /** Map String(friendly name) -> OIDInfo(info) */
110: private final static Map nameMap;
111:
112: static {
113: oidMap = new HashMap();
114: nameMap = new HashMap();
115: try {
116: addInternal(SUB_KEY_IDENTIFIER, "2.5.29.14",
117: "sun.security.x509.SubjectKeyIdentifierExtension");
118: addInternal(KEY_USAGE, "2.5.29.15",
119: "sun.security.x509.KeyUsageExtension");
120: addInternal(PRIVATE_KEY_USAGE, "2.5.29.16",
121: "sun.security.x509.PrivateKeyUsageExtension");
122: addInternal(SUB_ALT_NAME, "2.5.29.17",
123: "sun.security.x509.SubjectAlternativeNameExtension");
124: addInternal(ISSUER_ALT_NAME, "2.5.29.18",
125: "sun.security.x509.IssuerAlternativeNameExtension");
126: addInternal(BASIC_CONSTRAINTS, "2.5.29.19",
127: "sun.security.x509.BasicConstraintsExtension");
128: addInternal(CRL_NUMBER, "2.5.29.20",
129: "sun.security.x509.CRLNumberExtension");
130: addInternal(CRL_REASON, "2.5.29.21",
131: "sun.security.x509.CRLReasonCodeExtension");
132: addInternal(NAME_CONSTRAINTS, "2.5.29.30",
133: "sun.security.x509.NameConstraintsExtension");
134: addInternal(POLICY_MAPPINGS, "2.5.29.33",
135: "sun.security.x509.PolicyMappingsExtension");
136: addInternal(AUTH_KEY_IDENTIFIER, "2.5.29.35",
137: "sun.security.x509.AuthorityKeyIdentifierExtension");
138: addInternal(POLICY_CONSTRAINTS, "2.5.29.36",
139: "sun.security.x509.PolicyConstraintsExtension");
140: addInternal(NETSCAPE_CERT, "2.16.840.1.113730.1.1",
141: "sun.security.x509.NetscapeCertTypeExtension");
142: /* CDC/FP subsets away CertificatePoliciesExtension
143: * addInternal(CERT_POLICIES, "2.5.29.32",
144: * "sun.security.x509.CertificatePoliciesExtension");
145: */
146: addInternal(EXT_KEY_USAGE, "2.5.29.37",
147: "sun.security.x509.ExtendedKeyUsageExtension");
148: /*
149: * CDC/FP subsets InhibitAnyPolicyExtension and
150: * CRLDistributionPointsExtension to the
151: * security optional package.
152: addInternal(INHIBIT_ANY_POLICY, "2.5.29.54",
153: "sun.security.x509.InhibitAnyPolicyExtension");
154: addInternal(CRL_DIST_POINTS, "2.5.29.31",
155: "sun.security.x509.CRLDistributionPointsExtension");
156: */
157: } catch (IOException e) {
158: throw new RuntimeException("Internal error: " + e, e);
159: }
160: }
161:
162: /**
163: * Add attributes to the table. For internal use in the static
164: * initializer.
165: */
166: private static void addInternal(String name, String oidString,
167: String className) throws IOException {
168: ObjectIdentifier oid = new ObjectIdentifier(oidString);
169: OIDInfo info = new OIDInfo(name, oid, className);
170: oidMap.put(oid, info);
171: nameMap.put(name, info);
172: }
173:
174: /**
175: * Inner class encapsulating the mapping info and Class loading.
176: */
177: private static class OIDInfo {
178:
179: final ObjectIdentifier oid;
180: final String name;
181: final String className;
182: private volatile Class clazz;
183:
184: OIDInfo(String name, ObjectIdentifier oid, String className) {
185: this .name = name;
186: this .oid = oid;
187: this .className = className;
188: }
189:
190: OIDInfo(String name, ObjectIdentifier oid, Class clazz) {
191: this .name = name;
192: this .oid = oid;
193: this .className = clazz.getName();
194: this .clazz = clazz;
195: }
196:
197: /**
198: * Return the Class object associated with this attribute.
199: */
200: Class getClazz() throws CertificateException {
201: try {
202: Class c = clazz;
203: if (c == null) {
204: c = Class.forName(className);
205: clazz = c;
206: }
207: return c;
208: } catch (ClassNotFoundException e) {
209: throw (CertificateException) new CertificateException(
210: "Could not load class: " + e).initCause(e);
211: }
212: }
213: }
214:
215: /**
216: * Add a name to lookup table.
217: *
218: * @param name the name of the attr
219: * @param oid the string representation of the object identifier for
220: * the class.
221: * @param clazz the Class object associated with this attribute
222: * @exception CertificateException on errors.
223: */
224: public static void addAttribute(String name, String oid, Class clazz)
225: throws CertificateException {
226: ObjectIdentifier objId;
227: try {
228: objId = new ObjectIdentifier(oid);
229: } catch (IOException ioe) {
230: throw new CertificateException(
231: "Invalid Object identifier: " + oid);
232: }
233: OIDInfo info = new OIDInfo(name, objId, clazz);
234: if (oidMap.put(objId, info) != null) {
235: throw new CertificateException(
236: "Object identifier already exists: " + oid);
237: }
238: if (nameMap.put(name, info) != null) {
239: throw new CertificateException("Name already exists: "
240: + name);
241: }
242: }
243:
244: /**
245: * Return user friendly name associated with the OID.
246: *
247: * @param oid the name of the object identifier to be returned.
248: * @return the user friendly name or null if no name
249: * is registered for this oid.
250: */
251: public static String getName(ObjectIdentifier oid) {
252: OIDInfo info = (OIDInfo) oidMap.get(oid);
253: return (info == null) ? null : info.name;
254: }
255:
256: /**
257: * Return Object identifier for user friendly name.
258: *
259: * @param name the user friendly name.
260: * @return the Object Identifier or null if no oid
261: * is registered for this name.
262: */
263: public static ObjectIdentifier getOID(String name) {
264: OIDInfo info = (OIDInfo) nameMap.get(name);
265: return (info == null) ? null : info.oid;
266: }
267:
268: /**
269: * Return the java class object associated with the user friendly name.
270: *
271: * @param name the user friendly name.
272: * @exception CertificateException if class cannot be instantiated.
273: */
274: public static Class getClass(String name)
275: throws CertificateException {
276: OIDInfo info = (OIDInfo) nameMap.get(name);
277: return (info == null) ? null : info.getClazz();
278: }
279:
280: /**
281: * Return the java class object associated with the object identifier.
282: *
283: * @param oid the name of the object identifier to be returned.
284: * @exception CertificateException if class cannot be instatiated.
285: */
286: public static Class getClass(ObjectIdentifier oid)
287: throws CertificateException {
288: OIDInfo info = (OIDInfo) oidMap.get(oid);
289: return (info == null) ? null : info.getClazz();
290: }
291:
292: }
|