1 /* Security.java --- Java base security class implmentation
2 Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.security;
40 import java.io.FileInputStream;
41 import java.io.IOException;
42 import java.io.FileNotFoundException;
43 import java.security.Provider;
44 import java.util.Vector;
45 import java.util.Enumeration;
46 import java.util.Properties;
49 Security class that loads the Providers and provides an
50 interface to security properties.
52 @author Mark Benvenuto <ivymccough@worldnet.att.net>
55 public final class Security extends Object
57 private static Vector providers = new Vector();
58 private static Properties secprops;
62 loadProviders(System.getProperty("java.home"),
63 System.getProperty("java.vm.name"));
64 loadProviders(System.getProperty("gnu.classpath.home"), "classpath");
67 // This class can't be instantiated.
72 private static void loadProviders(String dir, String vendor)
74 if (dir == null || vendor == null)
77 String separator = System.getProperty("file.separator");
78 String secfilestr = (dir +
80 separator + "security" +
81 separator + vendor + ".security");
85 FileInputStream fin = new FileInputStream(secfilestr);
86 secprops = new Properties();
92 while ((name = secprops.getProperty("security.provider." + i++)) !=
95 Exception exception = null;
99 providers.addElement(Class.forName(name).newInstance());
102 catch (ClassNotFoundException x)
106 catch (InstantiationException x)
110 catch (IllegalAccessException x)
114 if (exception != null)
115 System.err.println ("Error loading security provider " + name
119 catch (FileNotFoundException ignored)
121 // Actually we probibly shouldn't ignore these, once the security
122 // properties file is actually installed somewhere.
124 catch (IOException ignored)
130 Gets a specific property for an algorithm. This is used to produce
131 specialized algorithm parsers.
133 @deprecated it used to a return the value of a propietary property
134 for the "SUN" Cryptographic Service Provider to obtain
135 algorithm-specific parameters. Used AlogorithmParameters and
138 @param algName name of algorithm to get property of
139 @param propName name of property to check
141 @return a string containing the value of the property
143 public static String getAlgorithmProperty(String algName, String propName)
145 /* TODO: Figure out what this actually does */
150 Adds a new provider, at a specified position. The position is the
151 preference order in which providers are searched for requested algorithms.
152 Note that it is not guaranteed that this preference will be respected. The
153 position is 1-based, that is, 1 is most preferred, followed by 2, and so
156 If the given provider is installed at the requested position, the
157 provider that used to be at that position, and all providers with a
158 position greater than position, are shifted up one position (towards the
159 end of the list of installed providers).
161 A provider cannot be added if it is already installed.
163 <b>NOT IMPLEMENTED YET:</b>[
164 First, if there is a security manager, its <code>checkSecurityAccess</code>
165 method is called with the string
166 <code>"insertProvider."+provider.getName()</code>
167 to see if it's ok to add a new provider. If the default implementation of
168 <code>checkSecurityAccess</code> is used (i.e., that method is not
169 overriden), then this will result in a call to the security manager's
170 <code>checkPermission</code> method with a <code>SecurityPermission(
171 "insertProvider."+provider.getName())</code> permission.]
173 @param provider the provider to be added.
174 @param position the preference position that the caller would like for
176 @return the actual preference position (1-based) in which the provider was
177 added, or -1 if the provider was not added because it is already installed.
178 @throws SecurityException if a security manager exists and its <code>
179 SecurityManager.checkSecurityAccess(java.lang.String)</code> method denies
180 access to add a new provider.
182 public static int insertProviderAt(Provider provider, int position)
184 SecurityManager sm = System.getSecurityManager();
186 sm.checkSecurityAccess("insertProvider." + provider.getName());
189 int max = providers.size ();
190 for (int i = 0; i < max; i++)
192 if (((Provider) providers.elementAt(i)).getName() ==
202 providers.insertElementAt(provider, position);
209 Adds a provider to the next position available.
211 <b>NOT IMPLEMENTED YET:</b> [
212 First, if there is a security manager, its <code>checkSecurityAccess</code>
213 method is called with the string
214 <code>"insertProvider."+provider.getName()</code>
215 to see if it's ok to add a new provider. If the default implementation of
216 <code>checkSecurityAccess</code> is used (i.e., that method is not
217 overriden), then this will result in a call to the security manager's
218 <code>checkPermission</code> method with a <code>SecurityPermission(
219 "insertProvider."+provider.getName())</code> permission.]
221 @param provider the provider to be added.
222 @return the preference position in which the provider was added, or <code>
223 -1</code> if the provider was not added because it is already installed.
224 @throws SecurityException if a security manager exists and its <code>
225 SecurityManager.checkSecurityAccess(java.lang.String)</code> method denies
226 access to add a new provider.
228 public static int addProvider(Provider provider)
230 return insertProviderAt (provider, providers.size () + 1);
234 Removes a provider. This allows dynamic unloading
235 of providers. It will automatically shift up providers to a higher
236 ranking. If the provider is not installed, it fails silently.
238 This method checks the security manager with the call checkSecurityAccess
239 with "removeProvider."+provider.getName() to see if the user can remove
242 @param name name of the provider to add
244 @throws SecurityException - if the security manager denies access to
245 remove a new provider
247 public static void removeProvider(String name)
249 SecurityManager sm = System.getSecurityManager();
251 sm.checkSecurityAccess("removeProvider." + name);
254 int max = providers.size ();
255 for (int i = 0; i < max; i++)
257 if (((Provider) providers.elementAt(i)).getName() == name)
266 Returns array containing all the providers. It is in the preference order
269 @return an array of installed providers
271 public static Provider[] getProviders()
273 Provider array[] = new Provider[providers.size ()];
274 providers.copyInto (array);
279 Returns the provider with the specified name. It will return null
280 if the provider cannot be found.
282 @param name name of the requested provider
284 @return requested provider
286 public static Provider getProvider(String name)
289 int max = providers.size ();
290 for (int i = 0; i < max; i++)
292 p = (Provider) providers.elementAt(i);
293 if (p.getName() == name)
300 Gets the value of a security property.
302 This method checks the security manager with the call checkSecurityAccess
303 with "getProperty."+key to see if the user can get this property.
305 @param key property to get
307 @return value of the property
309 @throws SecurityException - if the security manager denies access to
312 public static String getProperty(String key)
314 SecurityManager sm = System.getSecurityManager();
316 sm.checkSecurityAccess("getProperty." + key);
318 return secprops.getProperty(key);
323 Sets the value of a security property.
325 This method checks the security manager with the call checkSecurityAccess
326 with "setProperty."+key to see if the user can get this property.
328 @param key property to set
329 @param datnum new value of property
331 @throws SecurityException - if the security manager denies access to
334 public static void setProperty(String key, String datnum)
336 SecurityManager sm = System.getSecurityManager();
338 sm.checkSecurityAccess("setProperty." + key);
340 secprops.put(key, datnum);