PageRenderTime 99ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/sun/security/x509/OIDMap.java

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