Security.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Security.java --- Java base security class implmentation
  2. Copyright (C) 1999, 2001, 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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.io.File;
  33. import java.io.InputStream;
  34. import java.io.IOException;
  35. import java.io.FileNotFoundException;
  36. import java.net.URL;
  37. import java.security.Provider;
  38. import java.util.Vector;
  39. import java.util.Enumeration;
  40. import java.util.Properties;
  41. /**
  42. Security class that loads the Providers and provides an
  43. interface to security properties.
  44. @author Mark Benvenuto <ivymccough@worldnet.att.net>
  45. */
  46. public final class Security extends Object
  47. {
  48. private static Vector providers = new Vector();
  49. private static Properties secprops = new Properties();
  50. static
  51. {
  52. String base = System.getProperty("gnu.classpath.home.url");
  53. String vendor = System.getProperty("gnu.classpath.vm.shortname");
  54. // Try VM specific security file
  55. boolean loaded = loadProviders(base, vendor);
  56. // Append classpath standard provider if possible
  57. if (!loadProviders(base, "classpath") && !loaded && providers.size() == 0)
  58. {
  59. // No providers found and both security files failed to load properly.
  60. System.err.println
  61. ("WARNING: could not properly read security provider files:");
  62. System.err.println
  63. (" " + base + "/security/" + vendor + ".security");
  64. System.err.println
  65. (" " + base + "/security/" + "classpath" + ".security");
  66. System.err.println
  67. (" Falling back to standard GNU security provider");
  68. providers.addElement(new gnu.java.security.provider.Gnu());
  69. }
  70. }
  71. // This class can't be instantiated.
  72. private Security ()
  73. {
  74. }
  75. /**
  76. * Tries to load the vender specific security providers from the given
  77. * base URL. Returns true if the resource could be read and completely
  78. * parsed successfully, false otherwise.
  79. */
  80. private static boolean loadProviders(String baseUrl, String vendor)
  81. {
  82. if (baseUrl == null || vendor == null)
  83. return false;
  84. boolean result = true;
  85. String secfilestr = baseUrl + "/security/" + vendor + ".security";
  86. try
  87. {
  88. InputStream fin = new URL(secfilestr).openStream();
  89. secprops.load(fin);
  90. int i = 1;
  91. String name;
  92. while ((name = secprops.getProperty("security.provider." + i)) !=
  93. null)
  94. {
  95. Exception exception = null;
  96. try
  97. {
  98. providers.addElement(Class.forName(name).newInstance());
  99. }
  100. catch (ClassNotFoundException x)
  101. {
  102. exception = x;
  103. }
  104. catch (InstantiationException x)
  105. {
  106. exception = x;
  107. }
  108. catch (IllegalAccessException x)
  109. {
  110. exception = x;
  111. }
  112. if (exception != null)
  113. {
  114. System.err.println ("WARNING: Error loading security provider "
  115. + name + ": " + exception);
  116. result = false;
  117. }
  118. i++;
  119. }
  120. }
  121. catch (IOException ignored)
  122. {
  123. result = false;
  124. }
  125. return result;
  126. }
  127. /**
  128. Gets a specific property for an algorithm. This is used to produce
  129. specialized algorithm parsers.
  130. @deprecated it used to a return the value of a propietary property
  131. for the "SUN" Cryptographic Service Provider to obtain
  132. algorithm-specific parameters. Used AlogorithmParameters and
  133. KeyFactory instead.
  134. @param algName name of algorithm to get property of
  135. @param propName name of property to check
  136. @return a string containing the value of the property
  137. */
  138. public static String getAlgorithmProperty(String algName, String propName)
  139. {
  140. /* TODO: Figure out what this actually does */
  141. return null;
  142. }
  143. /**
  144. Adds a new provider, at a specified position. The position is the
  145. preference order in which providers are searched for requested algorithms.
  146. Note that it is not guaranteed that this preference will be respected. The
  147. position is 1-based, that is, 1 is most preferred, followed by 2, and so
  148. on.
  149. <p>
  150. If the given provider is installed at the requested position, the
  151. provider that used to be at that position, and all providers with a
  152. position greater than position, are shifted up one position (towards the
  153. end of the list of installed providers).
  154. <p>
  155. A provider cannot be added if it is already installed.
  156. <p>
  157. <b>NOT IMPLEMENTED YET:</b>[
  158. First, if there is a security manager, its <code>checkSecurityAccess</code>
  159. method is called with the string
  160. <code>"insertProvider."+provider.getName()</code>
  161. to see if it's ok to add a new provider. If the default implementation of
  162. <code>checkSecurityAccess</code> is used (i.e., that method is not
  163. overriden), then this will result in a call to the security manager's
  164. <code>checkPermission</code> method with a <code>SecurityPermission(
  165. "insertProvider."+provider.getName())</code> permission.]
  166. @param provider the provider to be added.
  167. @param position the preference position that the caller would like for
  168. this provider.
  169. @return the actual preference position (1-based) in which the provider was
  170. added, or -1 if the provider was not added because it is already installed.
  171. @throws SecurityException if a security manager exists and its <code>
  172. SecurityManager.checkSecurityAccess(java.lang.String)</code> method denies
  173. access to add a new provider.
  174. */
  175. public static int insertProviderAt(Provider provider, int position)
  176. {
  177. SecurityManager sm = System.getSecurityManager();
  178. if (sm != null)
  179. sm.checkSecurityAccess("insertProvider." + provider.getName());
  180. position--;
  181. int max = providers.size ();
  182. for (int i = 0; i < max; i++)
  183. {
  184. if (((Provider) providers.elementAt(i)).getName() ==
  185. provider.getName())
  186. return -1;
  187. }
  188. if (position < 0)
  189. position = 0;
  190. if (position > max)
  191. position = max;
  192. providers.insertElementAt(provider, position);
  193. return position + 1;
  194. }
  195. /**
  196. Adds a provider to the next position available.
  197. <p>
  198. <b>NOT IMPLEMENTED YET:</b> [
  199. First, if there is a security manager, its <code>checkSecurityAccess</code>
  200. method is called with the string
  201. <code>"insertProvider."+provider.getName()</code>
  202. to see if it's ok to add a new provider. If the default implementation of
  203. <code>checkSecurityAccess</code> is used (i.e., that method is not
  204. overriden), then this will result in a call to the security manager's
  205. <code>checkPermission</code> method with a <code>SecurityPermission(
  206. "insertProvider."+provider.getName())</code> permission.]
  207. @param provider the provider to be added.
  208. @return the preference position in which the provider was added, or <code>
  209. -1</code> if the provider was not added because it is already installed.
  210. @throws SecurityException if a security manager exists and its <code>
  211. SecurityManager.checkSecurityAccess(java.lang.String)</code> method denies
  212. access to add a new provider.
  213. */
  214. public static int addProvider(Provider provider)
  215. {
  216. return insertProviderAt (provider, providers.size () + 1);
  217. }
  218. /**
  219. Removes a provider. This allows dynamic unloading
  220. of providers. It will automatically shift up providers to a higher
  221. ranking. If the provider is not installed, it fails silently.
  222. This method checks the security manager with the call checkSecurityAccess
  223. with "removeProvider."+provider.getName() to see if the user can remove
  224. this provider.
  225. @param name name of the provider to add
  226. @throws SecurityException - if the security manager denies access to
  227. remove a new provider
  228. */
  229. public static void removeProvider(String name)
  230. {
  231. SecurityManager sm = System.getSecurityManager();
  232. if (sm != null)
  233. sm.checkSecurityAccess("removeProvider." + name);
  234. Provider p = null;
  235. int max = providers.size ();
  236. for (int i = 0; i < max; i++)
  237. {
  238. if (((Provider) providers.elementAt(i)).getName() == name)
  239. {
  240. providers.remove(i);
  241. break;
  242. }
  243. }
  244. }
  245. /**
  246. Returns array containing all the providers. It is in the preference order
  247. of the providers.
  248. @return an array of installed providers
  249. */
  250. public static Provider[] getProviders()
  251. {
  252. Provider array[] = new Provider[providers.size ()];
  253. providers.copyInto (array);
  254. return array;
  255. }
  256. /**
  257. Returns the provider with the specified name. It will return null
  258. if the provider cannot be found.
  259. @param name name of the requested provider
  260. @return requested provider
  261. */
  262. public static Provider getProvider(String name)
  263. {
  264. Provider p;
  265. int max = providers.size ();
  266. for (int i = 0; i < max; i++)
  267. {
  268. p = (Provider) providers.elementAt(i);
  269. if (p.getName() == name)
  270. return p;
  271. }
  272. return null;
  273. }
  274. /**
  275. Gets the value of a security property.
  276. This method checks the security manager with the call checkSecurityAccess
  277. with "getProperty."+key to see if the user can get this property.
  278. @param key property to get
  279. @return value of the property
  280. @throws SecurityException - if the security manager denies access to
  281. getting a property
  282. */
  283. public static String getProperty(String key)
  284. {
  285. SecurityManager sm = System.getSecurityManager();
  286. if (sm != null)
  287. sm.checkSecurityAccess("getProperty." + key);
  288. return secprops.getProperty(key);
  289. }
  290. /**
  291. Sets the value of a security property.
  292. This method checks the security manager with the call checkSecurityAccess
  293. with "setProperty."+key to see if the user can get this property.
  294. @param key property to set
  295. @param datnum new value of property
  296. @throws SecurityException - if the security manager denies access to
  297. setting a property
  298. */
  299. public static void setProperty(String key, String datnum)
  300. {
  301. SecurityManager sm = System.getSecurityManager();
  302. if (sm != null)
  303. sm.checkSecurityAccess("setProperty." + key);
  304. secprops.put(key, datnum);
  305. }
  306. }