OSDN Git Service

Integrate work by Raif S. Naffah (raif@fl.net.au)
[pf3gnuchains/gcc-fork.git] / libjava / java / security / Security.java
1 /* Security.java --- Java base security class implmentation
2    Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307 USA.
20
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
24 combination.
25
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. */
37
38 package java.security;
39 import java.io.File;
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;
47
48 /**
49    Security class that loads the Providers and provides an 
50    interface to security properties.
51
52    @author Mark Benvenuto <ivymccough@worldnet.att.net>
53  */
54
55 public final class Security extends Object
56 {
57   private static Vector providers = new Vector();
58   private static Properties secprops;
59
60   static
61   {
62     loadProviders(System.getProperty("java.home"),
63                   System.getProperty("java.vm.name"));
64     loadProviders(System.getProperty("gnu.classpath.home"), "classpath");
65   }
66
67   // This class can't be instantiated.
68   private Security ()
69   {
70   }
71
72   private static void loadProviders(String dir, String vendor)
73   {
74     if (dir == null || vendor == null)
75       return;
76
77     String separator = System.getProperty("file.separator");
78     String secfilestr = (dir +
79                          separator + "lib" +
80                          separator + "security" +
81                          separator + vendor + ".security");
82
83     try
84       {
85         FileInputStream fin = new FileInputStream(secfilestr);
86         secprops = new Properties();
87         secprops.load(fin);
88
89         int i = 1;
90         String name;
91
92         while ((name = secprops.getProperty("security.provider." + i++)) !=
93                null)
94           {
95             Exception exception = null;
96
97             try
98               {
99                 providers.addElement(Class.forName(name).newInstance());
100                 i++;
101               }
102             catch (ClassNotFoundException x)
103               {
104                 exception = x;
105               }
106             catch (InstantiationException x)
107               {
108                 exception = x;
109               }
110             catch (IllegalAccessException x)
111               {
112                 exception = x;
113               }
114             if (exception != null)
115               System.err.println ("Error loading security provider " + name
116                                   + ": " + exception);
117           }
118       }
119     catch (FileNotFoundException ignored)
120       {
121         // Actually we probibly shouldn't ignore these, once the security
122         // properties file is actually installed somewhere.
123       }
124     catch (IOException ignored)
125       {
126       }
127   }
128
129   /**
130      Gets a specific property for an algorithm. This is used to produce
131      specialized algorithm parsers.
132
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 
136      KeyFactory instead.
137
138      @param algName name of algorithm to get property of 
139      @param propName name of property to check
140
141      @return a string containing the value of the property
142    */
143   public static String getAlgorithmProperty(String algName, String propName)
144   {
145     /* TODO: Figure out what this actually does */
146     return null;
147   }
148
149   /**
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
154      on.
155      <p>
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).
160      <p>
161      A provider cannot be added if it is already installed.
162      <p>
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.]
172
173      @param provider the provider to be added.
174      @param position the preference position that the caller would like for
175      this provider.
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.
181    */
182   public static int insertProviderAt(Provider provider, int position)
183   {
184     SecurityManager sm = System.getSecurityManager();
185     if (sm != null)
186       sm.checkSecurityAccess("insertProvider." + provider.getName());
187
188     position--;
189     int max = providers.size ();
190     for (int i = 0; i < max; i++)
191       {
192         if (((Provider) providers.elementAt(i)).getName() ==
193             provider.getName())
194           return -1;
195       }
196
197     if (position < 0)
198       position = 0;
199     if (position > max)
200       position = max;
201
202     providers.insertElementAt(provider, position);
203
204     return position + 1;
205   }
206
207
208   /**
209      Adds a provider to the next position available.
210      <p>
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.]
220
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.
227    */
228   public static int addProvider(Provider provider)
229   {
230     return insertProviderAt (provider, providers.size () + 1);
231   }
232
233   /**
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.
237
238      This method checks the security manager with the call checkSecurityAccess
239      with "removeProvider."+provider.getName() to see if the user can remove
240      this provider.
241
242      @param name name of the provider to add
243
244      @throws SecurityException - if the security manager denies access to
245      remove a new provider
246    */
247   public static void removeProvider(String name)
248   {
249     SecurityManager sm = System.getSecurityManager();
250     if (sm != null)
251       sm.checkSecurityAccess("removeProvider." + name);
252
253     Provider p = null;
254     int max = providers.size ();
255     for (int i = 0; i < max; i++)
256       {
257         if (((Provider) providers.elementAt(i)).getName() == name)
258           {
259             providers.remove(i);
260             break;
261           }
262       }
263   }
264
265   /**
266      Returns array containing all the providers. It is in the preference order 
267      of the providers.
268
269      @return an array of installed providers
270    */
271   public static Provider[] getProviders()
272   {
273     Provider array[] = new Provider[providers.size ()];
274     providers.copyInto (array);
275     return array;
276   }
277
278   /**
279      Returns the provider with the specified name. It will return null 
280      if the provider cannot be found. 
281
282      @param name name of the requested provider
283
284      @return requested provider
285    */
286   public static Provider getProvider(String name)
287   {
288     Provider p;
289     int max = providers.size ();
290     for (int i = 0; i < max; i++)
291       {
292         p = (Provider) providers.elementAt(i);
293         if (p.getName() == name)
294           return p;
295       }
296     return null;
297   }
298
299   /**
300      Gets the value of a security property.
301
302      This method checks the security manager with the call checkSecurityAccess
303      with "getProperty."+key to see if the user can get this property.
304
305      @param key property to get
306
307      @return value of the property      
308
309      @throws SecurityException - if the security manager denies access to 
310      getting a property
311    */
312   public static String getProperty(String key)
313   {
314     SecurityManager sm = System.getSecurityManager();
315     if (sm != null)
316       sm.checkSecurityAccess("getProperty." + key);
317
318     return secprops.getProperty(key);
319   }
320
321
322   /**
323      Sets the value of a security property.
324
325      This method checks the security manager with the call checkSecurityAccess
326      with "setProperty."+key to see if the user can get this property.
327
328      @param key property to set
329      @param datnum new value of property
330
331      @throws SecurityException - if the security manager denies access to 
332      setting a property
333    */
334   public static void setProperty(String key, String datnum)
335   {
336     SecurityManager sm = System.getSecurityManager();
337     if (sm != null)
338       sm.checkSecurityAccess("setProperty." + key);
339
340     secprops.put(key, datnum);
341   }
342 }