1 /* 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.x509; 27 28 import java.util.*; 29 import java.io.IOException; 30 31 import java.security.cert.CertificateException; 32 33 import sun.security.util.*; 34 35 /** 36 * This class defines the mapping from OID & name to classes and vice 37 * versa. Used by CertificateExtensions & PKCS10 to get the java 38 * classes associated with a particular OID/name. 39 * 40 * @author Amit Kapoor 41 * @author Hemma Prafullchandra 42 * @author Andreas Sterbenz 43 * 44 */ 45 public class OIDMap { 46 47 private OIDMap() { 48 // empty 49 } 50 51 // "user-friendly" names 52 private static final String ROOT = X509CertImpl.NAME + "." + 53 X509CertInfo.NAME + "." + 54 X509CertInfo.EXTENSIONS; 55 private static final String AUTH_KEY_IDENTIFIER = ROOT + "." + 56 AuthorityKeyIdentifierExtension.NAME; 57 private static final String SUB_KEY_IDENTIFIER = ROOT + "." + 58 SubjectKeyIdentifierExtension.NAME; 59 private static final String KEY_USAGE = ROOT + "." + 60 KeyUsageExtension.NAME; 61 private static final String PRIVATE_KEY_USAGE = ROOT + "." + 62 PrivateKeyUsageExtension.NAME; 63 private static final String POLICY_MAPPINGS = ROOT + "." + 64 PolicyMappingsExtension.NAME; 65 private static final String SUB_ALT_NAME = ROOT + "." + 66 SubjectAlternativeNameExtension.NAME; 67 private static final String ISSUER_ALT_NAME = ROOT + "." + 68 IssuerAlternativeNameExtension.NAME; 69 private static final String BASIC_CONSTRAINTS = ROOT + "." + 70 BasicConstraintsExtension.NAME; 71 private static final String NAME_CONSTRAINTS = ROOT + "." + 72 NameConstraintsExtension.NAME; 73 private static final String POLICY_CONSTRAINTS = ROOT + "." + 74 PolicyConstraintsExtension.NAME; 75 private static final String CRL_NUMBER = ROOT + "." + 76 CRLNumberExtension.NAME; 77 private static final String CRL_REASON = ROOT + "." + 78 CRLReasonCodeExtension.NAME; 79 private static final String NETSCAPE_CERT = ROOT + "." + 80 NetscapeCertTypeExtension.NAME; 81 private static final String CERT_POLICIES = ROOT + "." + 82 CertificatePoliciesExtension.NAME; 83 private static final String EXT_KEY_USAGE = ROOT + "." + 84 ExtendedKeyUsageExtension.NAME; 85 private static final String INHIBIT_ANY_POLICY = ROOT + "." + 86 InhibitAnyPolicyExtension.NAME; 87 private static final String CRL_DIST_POINTS = ROOT + "." + 88 CRLDistributionPointsExtension.NAME; 89 90 private static final String CERT_ISSUER = ROOT + "." + 91 CertificateIssuerExtension.NAME; 92 private static final String AUTH_INFO_ACCESS = ROOT + "." + 93 AuthorityInfoAccessExtension.NAME; 94 private static final String ISSUING_DIST_POINT = ROOT + "." + 95 IssuingDistributionPointExtension.NAME; 96 private static final String DELTA_CRL_INDICATOR = ROOT + "." + 97 DeltaCRLIndicatorExtension.NAME; 98 private static final String FRESHEST_CRL = ROOT + "." + 99 FreshestCRLExtension.NAME; 100 101 private static final int NetscapeCertType_data[] = 102 { 2, 16, 840, 1, 113730, 1, 1 }; 103 104 /** Map ObjectIdentifier(oid) -> OIDInfo(info) */ 105 private final static Map<ObjectIdentifier,OIDInfo> oidMap; 106 107 /** Map String(friendly name) -> OIDInfo(info) */ 108 private final static Map<String,OIDInfo> nameMap; 109 110 static { 111 oidMap = new HashMap<ObjectIdentifier,OIDInfo>(); 112 nameMap = new HashMap<String,OIDInfo>(); 113 addInternal(SUB_KEY_IDENTIFIER, PKIXExtensions.SubjectKey_Id, 114 "sun.security.x509.SubjectKeyIdentifierExtension"); 115 addInternal(KEY_USAGE, PKIXExtensions.KeyUsage_Id, 116 "sun.security.x509.KeyUsageExtension"); 117 addInternal(PRIVATE_KEY_USAGE, PKIXExtensions.PrivateKeyUsage_Id, 118 "sun.security.x509.PrivateKeyUsageExtension"); 119 addInternal(SUB_ALT_NAME, PKIXExtensions.SubjectAlternativeName_Id, 120 "sun.security.x509.SubjectAlternativeNameExtension"); 121 addInternal(ISSUER_ALT_NAME, PKIXExtensions.IssuerAlternativeName_Id, 122 "sun.security.x509.IssuerAlternativeNameExtension"); 123 addInternal(BASIC_CONSTRAINTS, PKIXExtensions.BasicConstraints_Id, 124 "sun.security.x509.BasicConstraintsExtension"); 125 addInternal(CRL_NUMBER, PKIXExtensions.CRLNumber_Id, 126 "sun.security.x509.CRLNumberExtension"); 127 addInternal(CRL_REASON, PKIXExtensions.ReasonCode_Id, 128 "sun.security.x509.CRLReasonCodeExtension"); 129 addInternal(NAME_CONSTRAINTS, PKIXExtensions.NameConstraints_Id, 130 "sun.security.x509.NameConstraintsExtension"); 131 addInternal(POLICY_MAPPINGS, PKIXExtensions.PolicyMappings_Id, 132 "sun.security.x509.PolicyMappingsExtension"); 133 addInternal(AUTH_KEY_IDENTIFIER, PKIXExtensions.AuthorityKey_Id, 134 "sun.security.x509.AuthorityKeyIdentifierExtension"); 135 addInternal(POLICY_CONSTRAINTS, PKIXExtensions.PolicyConstraints_Id, 136 "sun.security.x509.PolicyConstraintsExtension"); 137 addInternal(NETSCAPE_CERT, ObjectIdentifier.newInternal 138 (new int[] {2,16,840,1,113730,1,1}), 139 "sun.security.x509.NetscapeCertTypeExtension"); 140 addInternal(CERT_POLICIES, PKIXExtensions.CertificatePolicies_Id, 141 "sun.security.x509.CertificatePoliciesExtension"); 142 addInternal(EXT_KEY_USAGE, PKIXExtensions.ExtendedKeyUsage_Id, 143 "sun.security.x509.ExtendedKeyUsageExtension"); 144 addInternal(INHIBIT_ANY_POLICY, PKIXExtensions.InhibitAnyPolicy_Id, 145 "sun.security.x509.InhibitAnyPolicyExtension"); 146 addInternal(CRL_DIST_POINTS, PKIXExtensions.CRLDistributionPoints_Id, 147 "sun.security.x509.CRLDistributionPointsExtension"); 148 addInternal(CERT_ISSUER, PKIXExtensions.CertificateIssuer_Id, 149 "sun.security.x509.CertificateIssuerExtension"); 150 addInternal(AUTH_INFO_ACCESS, PKIXExtensions.AuthInfoAccess_Id, 151 "sun.security.x509.AuthorityInfoAccessExtension"); 152 addInternal(ISSUING_DIST_POINT, 153 PKIXExtensions.IssuingDistributionPoint_Id, 154 "sun.security.x509.IssuingDistributionPointExtension"); 155 addInternal(DELTA_CRL_INDICATOR, PKIXExtensions.DeltaCRLIndicator_Id, 156 "sun.security.x509.DeltaCRLIndicatorExtension"); 157 addInternal(FRESHEST_CRL, PKIXExtensions.FreshestCRL_Id, 158 "sun.security.x509.FreshestCRLExtension"); 159 } 160 161 /** 162 * Add attributes to the table. For internal use in the static 163 * initializer. 164 */ 165 private static void addInternal(String name, ObjectIdentifier oid, 166 String className) { 167 OIDInfo info = new OIDInfo(name, oid, className); 168 oidMap.put(oid, info); 169 nameMap.put(name, info); 170 } 171 172 /** 173 * Inner class encapsulating the mapping info and Class loading. 174 */ 175 private static class OIDInfo { 176 177 final ObjectIdentifier oid; 178 final String name; 179 final String className; 180 private volatile Class<?> clazz; 181 182 OIDInfo(String name, ObjectIdentifier oid, String className) { 183 this.name = name; 184 this.oid = oid; 185 this.className = className; 186 } 187 188 OIDInfo(String name, ObjectIdentifier oid, Class<?> clazz) { 189 this.name = name; 190 this.oid = oid; 191 this.className = clazz.getName(); 192 this.clazz = clazz; 193 } 194 195 /** 196 * Return the Class object associated with this attribute. 197 */ 198 Class<?> getClazz() throws CertificateException { 199 try { 200 Class<?> c = clazz; 201 if (c == null) { 202 c = Class.forName(className); 203 clazz = c; 204 } 205 return c; 206 } catch (ClassNotFoundException e) { 207 throw new CertificateException("Could not load class: " + e, e); 208 } 209 } 210 } 211 212 /** 213 * Add a name to lookup table. 214 * 215 * @param name the name of the attr 216 * @param oid the string representation of the object identifier for 217 * the class. 218 * @param clazz the Class object associated with this attribute 219 * @exception CertificateException on errors. 220 */ 221 public static void addAttribute(String name, String oid, Class<?> clazz) 222 throws CertificateException { 223 ObjectIdentifier objId; 224 try { 225 objId = new ObjectIdentifier(oid); 226 } catch (IOException ioe) { 227 throw new CertificateException 228 ("Invalid Object identifier: " + oid); 229 } 230 OIDInfo info = new OIDInfo(name, objId, clazz); 231 if (oidMap.put(objId, info) != null) { 232 throw new CertificateException 233 ("Object identifier already exists: " + oid); 234 } 235 if (nameMap.put(name, info) != null) { 236 throw new CertificateException("Name already exists: " + name); 237 } 238 } 239 240 /** 241 * Return user friendly name associated with the OID. 242 * 243 * @param oid the name of the object identifier to be returned. 244 * @return the user friendly name or null if no name 245 * is registered for this oid. 246 */ 247 public static String getName(ObjectIdentifier oid) { 248 OIDInfo info = oidMap.get(oid); 249 return (info == null) ? null : info.name; 250 } 251 252 /** 253 * Return Object identifier for user friendly name. 254 * 255 * @param name the user friendly name. 256 * @return the Object Identifier or null if no oid 257 * is registered for this name. 258 */ 259 public static ObjectIdentifier getOID(String name) { 260 OIDInfo info = nameMap.get(name); 261 return (info == null) ? null : info.oid; 262 } 263 264 /** 265 * Return the java class object associated with the user friendly name. 266 * 267 * @param name the user friendly name. 268 * @exception CertificateException if class cannot be instantiated. 269 */ 270 public static Class<?> getClass(String name) throws CertificateException { 271 OIDInfo info = nameMap.get(name); 272 return (info == null) ? null : info.getClazz(); 273 } 274 275 /** 276 * Return the java class object associated with the object identifier. 277 * 278 * @param oid the name of the object identifier to be returned. 279 * @exception CertificateException if class cannot be instatiated. 280 */ 281 public static Class<?> getClass(ObjectIdentifier oid) 282 throws CertificateException { 283 OIDInfo info = oidMap.get(oid); 284 return (info == null) ? null : info.getClazz(); 285 } 286 287 }