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