J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | import java.security.cert.CertificateParsingException; |
| 33 | |
| 34 | import sun.security.util.*; |
| 35 | |
| 36 | /** |
| 37 | * This class defines the mapping from OID & name to classes and vice |
| 38 | * versa. Used by CertificateExtensions & PKCS10 to get the java |
| 39 | * classes associated with a particular OID/name. |
| 40 | * |
| 41 | * @author Amit Kapoor |
| 42 | * @author Hemma Prafullchandra |
| 43 | * @author Andreas Sterbenz |
| 44 | * |
| 45 | */ |
| 46 | public class OIDMap { |
| 47 | |
| 48 | private OIDMap() { |
| 49 | // empty |
| 50 | } |
| 51 | |
| 52 | // "user-friendly" names |
| 53 | private static final String ROOT = X509CertImpl.NAME + "." + |
| 54 | X509CertInfo.NAME + "." + |
| 55 | X509CertInfo.EXTENSIONS; |
| 56 | private static final String AUTH_KEY_IDENTIFIER = ROOT + "." + |
| 57 | AuthorityKeyIdentifierExtension.NAME; |
| 58 | private static final String SUB_KEY_IDENTIFIER = ROOT + "." + |
| 59 | SubjectKeyIdentifierExtension.NAME; |
| 60 | private static final String KEY_USAGE = ROOT + "." + |
| 61 | KeyUsageExtension.NAME; |
| 62 | private static final String PRIVATE_KEY_USAGE = ROOT + "." + |
| 63 | PrivateKeyUsageExtension.NAME; |
| 64 | private static final String POLICY_MAPPINGS = ROOT + "." + |
| 65 | PolicyMappingsExtension.NAME; |
| 66 | private static final String SUB_ALT_NAME = ROOT + "." + |
| 67 | SubjectAlternativeNameExtension.NAME; |
| 68 | private static final String ISSUER_ALT_NAME = ROOT + "." + |
| 69 | IssuerAlternativeNameExtension.NAME; |
| 70 | private static final String BASIC_CONSTRAINTS = ROOT + "." + |
| 71 | BasicConstraintsExtension.NAME; |
| 72 | private static final String NAME_CONSTRAINTS = ROOT + "." + |
| 73 | NameConstraintsExtension.NAME; |
| 74 | private static final String POLICY_CONSTRAINTS = ROOT + "." + |
| 75 | PolicyConstraintsExtension.NAME; |
| 76 | private static final String CRL_NUMBER = ROOT + "." + |
| 77 | CRLNumberExtension.NAME; |
| 78 | private static final String CRL_REASON = ROOT + "." + |
| 79 | CRLReasonCodeExtension.NAME; |
| 80 | private static final String NETSCAPE_CERT = ROOT + "." + |
| 81 | NetscapeCertTypeExtension.NAME; |
| 82 | private static final String CERT_POLICIES = ROOT + "." + |
| 83 | CertificatePoliciesExtension.NAME; |
| 84 | private static final String EXT_KEY_USAGE = ROOT + "." + |
| 85 | ExtendedKeyUsageExtension.NAME; |
| 86 | private static final String INHIBIT_ANY_POLICY = ROOT + "." + |
| 87 | InhibitAnyPolicyExtension.NAME; |
| 88 | private static final String CRL_DIST_POINTS = ROOT + "." + |
| 89 | CRLDistributionPointsExtension.NAME; |
| 90 | |
| 91 | private static final String CERT_ISSUER = ROOT + "." + |
| 92 | CertificateIssuerExtension.NAME; |
| 93 | private static final String AUTH_INFO_ACCESS = ROOT + "." + |
| 94 | AuthorityInfoAccessExtension.NAME; |
| 95 | private static final String ISSUING_DIST_POINT = ROOT + "." + |
| 96 | IssuingDistributionPointExtension.NAME; |
| 97 | private static final String DELTA_CRL_INDICATOR = ROOT + "." + |
| 98 | DeltaCRLIndicatorExtension.NAME; |
| 99 | private static final String FRESHEST_CRL = ROOT + "." + |
| 100 | FreshestCRLExtension.NAME; |
| 101 | |
| 102 | private static final int NetscapeCertType_data[] = |
| 103 | { 2, 16, 840, 1, 113730, 1, 1 }; |
| 104 | |
| 105 | /** Map ObjectIdentifier(oid) -> OIDInfo(info) */ |
| 106 | private final static Map<ObjectIdentifier,OIDInfo> oidMap; |
| 107 | |
| 108 | /** Map String(friendly name) -> OIDInfo(info) */ |
| 109 | private final static Map<String,OIDInfo> nameMap; |
| 110 | |
| 111 | static { |
| 112 | oidMap = new HashMap<ObjectIdentifier,OIDInfo>(); |
| 113 | nameMap = new HashMap<String,OIDInfo>(); |
| 114 | addInternal(SUB_KEY_IDENTIFIER, PKIXExtensions.SubjectKey_Id, |
| 115 | "sun.security.x509.SubjectKeyIdentifierExtension"); |
| 116 | addInternal(KEY_USAGE, PKIXExtensions.KeyUsage_Id, |
| 117 | "sun.security.x509.KeyUsageExtension"); |
| 118 | addInternal(PRIVATE_KEY_USAGE, PKIXExtensions.PrivateKeyUsage_Id, |
| 119 | "sun.security.x509.PrivateKeyUsageExtension"); |
| 120 | addInternal(SUB_ALT_NAME, PKIXExtensions.SubjectAlternativeName_Id, |
| 121 | "sun.security.x509.SubjectAlternativeNameExtension"); |
| 122 | addInternal(ISSUER_ALT_NAME, PKIXExtensions.IssuerAlternativeName_Id, |
| 123 | "sun.security.x509.IssuerAlternativeNameExtension"); |
| 124 | addInternal(BASIC_CONSTRAINTS, PKIXExtensions.BasicConstraints_Id, |
| 125 | "sun.security.x509.BasicConstraintsExtension"); |
| 126 | addInternal(CRL_NUMBER, PKIXExtensions.CRLNumber_Id, |
| 127 | "sun.security.x509.CRLNumberExtension"); |
| 128 | addInternal(CRL_REASON, PKIXExtensions.ReasonCode_Id, |
| 129 | "sun.security.x509.CRLReasonCodeExtension"); |
| 130 | addInternal(NAME_CONSTRAINTS, PKIXExtensions.NameConstraints_Id, |
| 131 | "sun.security.x509.NameConstraintsExtension"); |
| 132 | addInternal(POLICY_MAPPINGS, PKIXExtensions.PolicyMappings_Id, |
| 133 | "sun.security.x509.PolicyMappingsExtension"); |
| 134 | addInternal(AUTH_KEY_IDENTIFIER, PKIXExtensions.AuthorityKey_Id, |
| 135 | "sun.security.x509.AuthorityKeyIdentifierExtension"); |
| 136 | addInternal(POLICY_CONSTRAINTS, PKIXExtensions.PolicyConstraints_Id, |
| 137 | "sun.security.x509.PolicyConstraintsExtension"); |
| 138 | addInternal(NETSCAPE_CERT, ObjectIdentifier.newInternal |
| 139 | (new int[] {2,16,840,1,113730,1,1}), |
| 140 | "sun.security.x509.NetscapeCertTypeExtension"); |
| 141 | addInternal(CERT_POLICIES, PKIXExtensions.CertificatePolicies_Id, |
| 142 | "sun.security.x509.CertificatePoliciesExtension"); |
| 143 | addInternal(EXT_KEY_USAGE, PKIXExtensions.ExtendedKeyUsage_Id, |
| 144 | "sun.security.x509.ExtendedKeyUsageExtension"); |
| 145 | addInternal(INHIBIT_ANY_POLICY, PKIXExtensions.InhibitAnyPolicy_Id, |
| 146 | "sun.security.x509.InhibitAnyPolicyExtension"); |
| 147 | addInternal(CRL_DIST_POINTS, PKIXExtensions.CRLDistributionPoints_Id, |
| 148 | "sun.security.x509.CRLDistributionPointsExtension"); |
| 149 | addInternal(CERT_ISSUER, PKIXExtensions.CertificateIssuer_Id, |
| 150 | "sun.security.x509.CertificateIssuerExtension"); |
| 151 | addInternal(AUTH_INFO_ACCESS, PKIXExtensions.AuthInfoAccess_Id, |
| 152 | "sun.security.x509.AuthorityInfoAccessExtension"); |
| 153 | addInternal(ISSUING_DIST_POINT, |
| 154 | PKIXExtensions.IssuingDistributionPoint_Id, |
| 155 | "sun.security.x509.IssuingDistributionPointExtension"); |
| 156 | addInternal(DELTA_CRL_INDICATOR, PKIXExtensions.DeltaCRLIndicator_Id, |
| 157 | "sun.security.x509.DeltaCRLIndicatorExtension"); |
| 158 | addInternal(FRESHEST_CRL, PKIXExtensions.FreshestCRL_Id, |
| 159 | "sun.security.x509.FreshestCRLExtension"); |
| 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, ObjectIdentifier oid, |
| 167 | String className) { |
| 168 | OIDInfo info = new OIDInfo(name, oid, className); |
| 169 | oidMap.put(oid, info); |
| 170 | nameMap.put(name, info); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Inner class encapsulating the mapping info and Class loading. |
| 175 | */ |
| 176 | private static class OIDInfo { |
| 177 | |
| 178 | final ObjectIdentifier oid; |
| 179 | final String name; |
| 180 | final String className; |
| 181 | private volatile Class clazz; |
| 182 | |
| 183 | OIDInfo(String name, ObjectIdentifier oid, String className) { |
| 184 | this.name = name; |
| 185 | this.oid = oid; |
| 186 | this.className = className; |
| 187 | } |
| 188 | |
| 189 | OIDInfo(String name, ObjectIdentifier oid, Class clazz) { |
| 190 | this.name = name; |
| 191 | this.oid = oid; |
| 192 | this.className = clazz.getName(); |
| 193 | this.clazz = clazz; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Return the Class object associated with this attribute. |
| 198 | */ |
| 199 | Class getClazz() throws CertificateException { |
| 200 | try { |
| 201 | Class c = clazz; |
| 202 | if (c == null) { |
| 203 | c = Class.forName(className); |
| 204 | clazz = c; |
| 205 | } |
| 206 | return c; |
| 207 | } catch (ClassNotFoundException e) { |
| 208 | throw (CertificateException)new CertificateException |
| 209 | ("Could not load class: " + e).initCause(e); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Add a name to lookup table. |
| 216 | * |
| 217 | * @param name the name of the attr |
| 218 | * @param oid the string representation of the object identifier for |
| 219 | * the class. |
| 220 | * @param clazz the Class object associated with this attribute |
| 221 | * @exception CertificateException on errors. |
| 222 | */ |
| 223 | public static void addAttribute(String name, String oid, Class clazz) |
| 224 | throws CertificateException { |
| 225 | ObjectIdentifier objId; |
| 226 | try { |
| 227 | objId = new ObjectIdentifier(oid); |
| 228 | } catch (IOException ioe) { |
| 229 | throw new CertificateException |
| 230 | ("Invalid Object identifier: " + oid); |
| 231 | } |
| 232 | OIDInfo info = new OIDInfo(name, objId, clazz); |
| 233 | if (oidMap.put(objId, info) != null) { |
| 234 | throw new CertificateException |
| 235 | ("Object identifier already exists: " + oid); |
| 236 | } |
| 237 | if (nameMap.put(name, info) != null) { |
| 238 | throw new CertificateException("Name already exists: " + name); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Return user friendly name associated with the OID. |
| 244 | * |
| 245 | * @param oid the name of the object identifier to be returned. |
| 246 | * @return the user friendly name or null if no name |
| 247 | * is registered for this oid. |
| 248 | */ |
| 249 | public static String getName(ObjectIdentifier oid) { |
| 250 | OIDInfo info = oidMap.get(oid); |
| 251 | return (info == null) ? null : info.name; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Return Object identifier for user friendly name. |
| 256 | * |
| 257 | * @param name the user friendly name. |
| 258 | * @return the Object Identifier or null if no oid |
| 259 | * is registered for this name. |
| 260 | */ |
| 261 | public static ObjectIdentifier getOID(String name) { |
| 262 | OIDInfo info = nameMap.get(name); |
| 263 | return (info == null) ? null : info.oid; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Return the java class object associated with the user friendly name. |
| 268 | * |
| 269 | * @param name the user friendly name. |
| 270 | * @exception CertificateException if class cannot be instantiated. |
| 271 | */ |
| 272 | public static Class getClass(String name) throws CertificateException { |
| 273 | OIDInfo info = nameMap.get(name); |
| 274 | return (info == null) ? null : info.getClazz(); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Return the java class object associated with the object identifier. |
| 279 | * |
| 280 | * @param oid the name of the object identifier to be returned. |
| 281 | * @exception CertificateException if class cannot be instatiated. |
| 282 | */ |
| 283 | public static Class getClass(ObjectIdentifier oid) |
| 284 | throws CertificateException { |
| 285 | OIDInfo info = oidMap.get(oid); |
| 286 | return (info == null) ? null : info.getClazz(); |
| 287 | } |
| 288 | |
| 289 | } |