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
.InputStream
;
41 import java
.io
.IOException
;
42 import java
.io
.FileNotFoundException
;
44 import java
.security
.Provider
;
45 import java
.util
.Vector
;
46 import java
.util
.Enumeration
;
47 import java
.util
.Properties
;
50 Security class that loads the Providers and provides an
51 interface to security properties.
53 @author Mark Benvenuto <ivymccough@worldnet.att.net>
56 public final class Security
extends Object
58 private static Vector providers
= new Vector();
59 private static Properties secprops
;
63 String base
= System
.getProperty("gnu.classpath.home.url");
64 loadProviders(base
, System
.getProperty("gnu.classpath.vm.shortname"));
65 loadProviders(base
, "classpath");
68 // This class can't be instantiated.
73 private static void loadProviders(String baseUrl
, String vendor
)
75 if (baseUrl
== null || vendor
== null)
78 String secfilestr
= baseUrl
+ "/security/" + vendor
+ ".security";
82 InputStream fin
= new URL(secfilestr
).openStream();
83 secprops
= new Properties();
89 while ((name
= secprops
.getProperty("security.provider." + i
)) !=
92 Exception exception
= null;
96 providers
.addElement(Class
.forName(name
).newInstance());
98 catch (ClassNotFoundException x
)
102 catch (InstantiationException x
)
106 catch (IllegalAccessException x
)
110 if (exception
!= null)
111 System
.err
.println ("Error loading security provider " + name
116 catch (FileNotFoundException ignored
)
118 // Actually we probibly shouldn't ignore these, once the security
119 // properties file is actually installed somewhere.
121 catch (IOException ignored
)
127 Gets a specific property for an algorithm. This is used to produce
128 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
135 @param algName name of algorithm to get property of
136 @param propName name of property to check
138 @return a string containing the value of the property
140 public static String
getAlgorithmProperty(String algName
, String propName
)
142 /* TODO: Figure out what this actually does */
147 Adds a new provider, at a specified position. The position is the
148 preference order in which providers are searched for requested algorithms.
149 Note that it is not guaranteed that this preference will be respected. The
150 position is 1-based, that is, 1 is most preferred, followed by 2, and so
153 If the given provider is installed at the requested position, the
154 provider that used to be at that position, and all providers with a
155 position greater than position, are shifted up one position (towards the
156 end of the list of installed providers).
158 A provider cannot be added if it is already installed.
160 <b>NOT IMPLEMENTED YET:</b>[
161 First, if there is a security manager, its <code>checkSecurityAccess</code>
162 method is called with the string
163 <code>"insertProvider."+provider.getName()</code>
164 to see if it's ok to add a new provider. If the default implementation of
165 <code>checkSecurityAccess</code> is used (i.e., that method is not
166 overriden), then this will result in a call to the security manager's
167 <code>checkPermission</code> method with a <code>SecurityPermission(
168 "insertProvider."+provider.getName())</code> permission.]
170 @param provider the provider to be added.
171 @param position the preference position that the caller would like for
173 @return the actual preference position (1-based) in which the provider was
174 added, or -1 if the provider was not added because it is already installed.
175 @throws SecurityException if a security manager exists and its <code>
176 SecurityManager.checkSecurityAccess(java.lang.String)</code> method denies
177 access to add a new provider.
179 public static int insertProviderAt(Provider provider
, int position
)
181 SecurityManager sm
= System
.getSecurityManager();
183 sm
.checkSecurityAccess("insertProvider." + provider
.getName());
186 int max
= providers
.size ();
187 for (int i
= 0; i
< max
; i
++)
189 if (((Provider
) providers
.elementAt(i
)).getName() ==
199 providers
.insertElementAt(provider
, position
);
206 Adds a provider to the next position available.
208 <b>NOT IMPLEMENTED YET:</b> [
209 First, if there is a security manager, its <code>checkSecurityAccess</code>
210 method is called with the string
211 <code>"insertProvider."+provider.getName()</code>
212 to see if it's ok to add a new provider. If the default implementation of
213 <code>checkSecurityAccess</code> is used (i.e., that method is not
214 overriden), then this will result in a call to the security manager's
215 <code>checkPermission</code> method with a <code>SecurityPermission(
216 "insertProvider."+provider.getName())</code> permission.]
218 @param provider the provider to be added.
219 @return the preference position in which the provider was added, or <code>
220 -1</code> if the provider was not added because it is already installed.
221 @throws SecurityException if a security manager exists and its <code>
222 SecurityManager.checkSecurityAccess(java.lang.String)</code> method denies
223 access to add a new provider.
225 public static int addProvider(Provider provider
)
227 return insertProviderAt (provider
, providers
.size () + 1);
231 Removes a provider. This allows dynamic unloading
232 of providers. It will automatically shift up providers to a higher
233 ranking. If the provider is not installed, it fails silently.
235 This method checks the security manager with the call checkSecurityAccess
236 with "removeProvider."+provider.getName() to see if the user can remove
239 @param name name of the provider to add
241 @throws SecurityException - if the security manager denies access to
242 remove a new provider
244 public static void removeProvider(String name
)
246 SecurityManager sm
= System
.getSecurityManager();
248 sm
.checkSecurityAccess("removeProvider." + name
);
251 int max
= providers
.size ();
252 for (int i
= 0; i
< max
; i
++)
254 if (((Provider
) providers
.elementAt(i
)).getName() == name
)
263 Returns array containing all the providers. It is in the preference order
266 @return an array of installed providers
268 public static Provider
[] getProviders()
270 Provider array
[] = new Provider
[providers
.size ()];
271 providers
.copyInto (array
);
276 Returns the provider with the specified name. It will return null
277 if the provider cannot be found.
279 @param name name of the requested provider
281 @return requested provider
283 public static Provider
getProvider(String name
)
286 int max
= providers
.size ();
287 for (int i
= 0; i
< max
; i
++)
289 p
= (Provider
) providers
.elementAt(i
);
290 if (p
.getName() == name
)
297 Gets the value of a security property.
299 This method checks the security manager with the call checkSecurityAccess
300 with "getProperty."+key to see if the user can get this property.
302 @param key property to get
304 @return value of the property
306 @throws SecurityException - if the security manager denies access to
309 public static String
getProperty(String key
)
311 SecurityManager sm
= System
.getSecurityManager();
313 sm
.checkSecurityAccess("getProperty." + key
);
315 return secprops
.getProperty(key
);
320 Sets the value of a security property.
322 This method checks the security manager with the call checkSecurityAccess
323 with "setProperty."+key to see if the user can get this property.
325 @param key property to set
326 @param datnum new value of property
328 @throws SecurityException - if the security manager denies access to
331 public static void setProperty(String key
, String datnum
)
333 SecurityManager sm
= System
.getSecurityManager();
335 sm
.checkSecurityAccess("setProperty." + key
);
337 secprops
.put(key
, datnum
);