SecureClassLoader.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* SecureClassLoader.java --- A Secure Class Loader
  2. Copyright (C) 1999, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. import java.nio.ByteBuffer;
  33. import java.util.HashMap;
  34. /**
  35. * A Secure Class Loader for loading classes with additional
  36. * support for specifying code source and permissions when
  37. * they are retrieved by the system policy handler.
  38. *
  39. * @since 1.2
  40. *
  41. * @author Mark Benvenuto
  42. */
  43. public class SecureClassLoader extends ClassLoader
  44. {
  45. private final HashMap<CodeSource,ProtectionDomain> protectionDomainCache
  46. = new HashMap<CodeSource, ProtectionDomain>();
  47. protected SecureClassLoader(ClassLoader parent)
  48. {
  49. super(parent);
  50. }
  51. protected SecureClassLoader()
  52. {
  53. }
  54. /**
  55. * Creates a class using an array of bytes and a
  56. * CodeSource.
  57. *
  58. * @param name the name to give the class. null if unknown.
  59. * @param b the data representing the classfile, in classfile format.
  60. * @param off the offset into the data where the classfile starts.
  61. * @param len the length of the classfile data in the array.
  62. * @param cs the CodeSource for the class or null when unknown.
  63. *
  64. * @return the class that was defined and optional CodeSource.
  65. *
  66. * @exception ClassFormatError if the byte array is not in proper classfile format.
  67. */
  68. protected final Class<?> defineClass(String name, byte[] b, int off, int len,
  69. CodeSource cs)
  70. {
  71. return super.defineClass(name, b, off, len, getProtectionDomain(cs));
  72. }
  73. /**
  74. * Creates a class using an ByteBuffer and a
  75. * CodeSource.
  76. *
  77. * @param name the name to give the class. null if unknown.
  78. * @param b the data representing the classfile, in classfile format.
  79. * @param cs the CodeSource for the class or null when unknown.
  80. *
  81. * @return the class that was defined and optional CodeSource.
  82. *
  83. * @exception ClassFormatError if the byte array is not in proper classfile format.
  84. *
  85. * @since 1.5
  86. */
  87. protected final Class<?> defineClass(String name, ByteBuffer b, CodeSource cs)
  88. {
  89. return super.defineClass(name, b, getProtectionDomain(cs));
  90. }
  91. /* Lookup or create a protection domain for the CodeSource,
  92. * if CodeSource is null it will return null. */
  93. private ProtectionDomain getProtectionDomain(CodeSource cs)
  94. {
  95. ProtectionDomain protectionDomain = null;
  96. if (cs != null)
  97. {
  98. synchronized (protectionDomainCache)
  99. {
  100. protectionDomain = protectionDomainCache.get(cs);
  101. }
  102. if (protectionDomain == null)
  103. {
  104. protectionDomain
  105. = new ProtectionDomain(cs, getPermissions(cs), this, null);
  106. synchronized (protectionDomainCache)
  107. {
  108. ProtectionDomain domain = protectionDomainCache.get(cs);
  109. if (domain == null)
  110. protectionDomainCache.put(cs, protectionDomain);
  111. else
  112. protectionDomain = domain;
  113. }
  114. }
  115. }
  116. return protectionDomain;
  117. }
  118. /**
  119. * Returns a PermissionCollection for the specified CodeSource.
  120. * The default implementation invokes
  121. * java.security.Policy.getPermissions.
  122. *
  123. * This method is called by defineClass that takes a CodeSource
  124. * argument to build a proper ProtectionDomain for the class
  125. * being defined.
  126. */
  127. protected PermissionCollection getPermissions(CodeSource cs)
  128. {
  129. Policy policy = Policy.getCurrentPolicy();
  130. return policy.getPermissions(cs);
  131. }
  132. }