| 1 | /* Security.java --- Java base security class implmentation
|
|---|
| 2 | Copyright (C) 1999, 2001 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 <[email protected]>
|
|---|
| 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.vm.name"));
|
|---|
| 63 | loadProviders("classpath");
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // This class can't be instantiated.
|
|---|
| 67 | private Security ()
|
|---|
| 68 | {
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | private static void loadProviders(String vendor)
|
|---|
| 72 | {
|
|---|
| 73 | if (vendor == null)
|
|---|
| 74 | return;
|
|---|
| 75 |
|
|---|
| 76 | String separator = System.getProperty("file.separator");
|
|---|
| 77 | String secfilestr = (System.getProperty("java.home") +
|
|---|
| 78 | separator + "lib" +
|
|---|
| 79 | separator + "security" +
|
|---|
| 80 | separator + vendor + ".security");
|
|---|
| 81 |
|
|---|
| 82 | try
|
|---|
| 83 | {
|
|---|
| 84 | FileInputStream fin = new FileInputStream(secfilestr);
|
|---|
| 85 | secprops = new Properties();
|
|---|
| 86 | secprops.load(fin);
|
|---|
| 87 |
|
|---|
| 88 | int i = 1;
|
|---|
| 89 | String name;
|
|---|
| 90 |
|
|---|
| 91 | while ((name = secprops.getProperty("security.provider." + i++)) !=
|
|---|
| 92 | null)
|
|---|
| 93 | {
|
|---|
| 94 | Exception exception = null;
|
|---|
| 95 |
|
|---|
| 96 | try
|
|---|
| 97 | {
|
|---|
| 98 | providers.addElement(Class.forName(name).newInstance());
|
|---|
| 99 | i++;
|
|---|
| 100 | }
|
|---|
| 101 | catch (ClassNotFoundException x)
|
|---|
| 102 | {
|
|---|
| 103 | exception = x;
|
|---|
| 104 | }
|
|---|
| 105 | catch (InstantiationException x)
|
|---|
| 106 | {
|
|---|
| 107 | exception = x;
|
|---|
| 108 | }
|
|---|
| 109 | catch (IllegalAccessException x)
|
|---|
| 110 | {
|
|---|
| 111 | exception = x;
|
|---|
| 112 | }
|
|---|
| 113 | if (exception != null)
|
|---|
| 114 | System.err.println ("Error loading security provider " + name
|
|---|
| 115 | + ": " + exception);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 | catch (FileNotFoundException ignored)
|
|---|
| 119 | {
|
|---|
| 120 | // Actually we probibly shouldn't ignore these, once the security
|
|---|
| 121 | // properties file is actually installed somewhere.
|
|---|
| 122 | }
|
|---|
| 123 | catch (IOException ignored)
|
|---|
| 124 | {
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | Gets a specific property for an algorithm. This is used to produce specialized
|
|---|
| 130 | algorithm parsers.
|
|---|
| 131 |
|
|---|
| 132 | @deprecated it used to a return the value of a propietary property
|
|---|
| 133 | for the "SUN" Cryptographic Service Provider to obtain
|
|---|
| 134 | algorithm-specific parameters. Used AlogorithmParameters and
|
|---|
| 135 | KeyFactory instead.
|
|---|
| 136 |
|
|---|
| 137 | @param algName name of algorithm to get property of
|
|---|
| 138 | @param propName name of property to check
|
|---|
| 139 |
|
|---|
| 140 | @return a string containing the value of the property
|
|---|
| 141 | */
|
|---|
| 142 | public static String getAlgorithmProperty(String algName, String propName)
|
|---|
| 143 | {
|
|---|
| 144 | /* TODO: Figure out what this actually does */
|
|---|
| 145 | return null;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | Adds a new provider at the specified position. This allows dynamic loading
|
|---|
| 150 | of providers. It will check for duplication of providers.
|
|---|
| 151 |
|
|---|
| 152 | This class checks the security manager with the call checkSecurityAccess
|
|---|
| 153 | with "insertProvider."+provider.getName() to see if the user can add this
|
|---|
| 154 | provider.
|
|---|
| 155 |
|
|---|
| 156 | @param provider the provider to add
|
|---|
| 157 | @param position position to add the provider at
|
|---|
| 158 |
|
|---|
| 159 | @return the position the provider was added at, or -1 if a duplicate provider
|
|---|
| 160 | was found
|
|---|
| 161 |
|
|---|
| 162 | @throws SecurityException - if the security manager denies access to add a
|
|---|
| 163 | new provider
|
|---|
| 164 | */
|
|---|
| 165 | public static int insertProviderAt(Provider provider, int position)
|
|---|
| 166 | {
|
|---|
| 167 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 168 | if (sm != null)
|
|---|
| 169 | sm.checkSecurityAccess("insertProvider." + provider.getName());
|
|---|
| 170 |
|
|---|
| 171 | int max = providers.size ();
|
|---|
| 172 | for (int i = 0; i < max; i++)
|
|---|
| 173 | {
|
|---|
| 174 | if (((Provider) providers.elementAt(i)).getName() ==
|
|---|
| 175 | provider.getName())
|
|---|
| 176 | return -1;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | if (position < 0)
|
|---|
| 180 | position = 0;
|
|---|
| 181 | if (position > max)
|
|---|
| 182 | position = max;
|
|---|
| 183 |
|
|---|
| 184 | providers.insertElementAt(provider, position);
|
|---|
| 185 |
|
|---|
| 186 | return position;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | Adds a new provider. This allows dynamic loading
|
|---|
| 192 | of providers. It will check for duplication of providers.
|
|---|
| 193 |
|
|---|
| 194 | This method checks the security manager with the call checkSecurityAccess
|
|---|
| 195 | with "insertProvider."+provider.getName() to see if the user can add this
|
|---|
| 196 | provider.
|
|---|
| 197 |
|
|---|
| 198 | @param provider the provider to add
|
|---|
| 199 |
|
|---|
| 200 | @return the position the provider was added at, or -1 if a duplicate provider
|
|---|
| 201 | was found
|
|---|
| 202 |
|
|---|
| 203 | @throws SecurityException - if the security manager denies access to add a
|
|---|
| 204 | new provider
|
|---|
| 205 | */
|
|---|
| 206 | public static int addProvider(Provider provider)
|
|---|
| 207 | {
|
|---|
| 208 | return insertProviderAt (provider, providers.size ());
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | Removes a provider. This allows dynamic unloading
|
|---|
| 213 | of providers. It will automatically shift up providers to a higher
|
|---|
| 214 | ranking. If the provider is not installed, it fails silently.
|
|---|
| 215 |
|
|---|
| 216 | This method checks the security manager with the call checkSecurityAccess
|
|---|
| 217 | with "removeProvider."+provider.getName() to see if the user can remove this
|
|---|
| 218 | provider.
|
|---|
| 219 |
|
|---|
| 220 | @param name name of the provider to add
|
|---|
| 221 |
|
|---|
| 222 | @throws SecurityException - if the security manager denies access to remove a
|
|---|
| 223 | new provider
|
|---|
| 224 | */
|
|---|
| 225 | public static void removeProvider(String name)
|
|---|
| 226 | {
|
|---|
| 227 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 228 | if (sm != null)
|
|---|
| 229 | sm.checkSecurityAccess("removeProvider." + name);
|
|---|
| 230 |
|
|---|
| 231 | Provider p = null;
|
|---|
| 232 | int max = providers.size ();
|
|---|
| 233 | for (int i = 0; i < max; i++)
|
|---|
| 234 | {
|
|---|
| 235 | if (((Provider) providers.elementAt(i)).getName() == name)
|
|---|
| 236 | {
|
|---|
| 237 | providers.remove(i);
|
|---|
| 238 | break;
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | /**
|
|---|
| 244 | Returns array containing all the providers. It is in the preference order
|
|---|
| 245 | of the providers.
|
|---|
| 246 |
|
|---|
| 247 | @return an array of installed providers
|
|---|
| 248 | */
|
|---|
| 249 | public static Provider[] getProviders()
|
|---|
| 250 | {
|
|---|
| 251 | Provider array[] = new Provider[providers.size ()];
|
|---|
| 252 | providers.copyInto (array);
|
|---|
| 253 | return array;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /**
|
|---|
| 257 | Returns the provider with the specified name. It will return null
|
|---|
| 258 | if the provider cannot be found.
|
|---|
| 259 |
|
|---|
| 260 | @param name name of the requested provider
|
|---|
| 261 |
|
|---|
| 262 | @return requested provider
|
|---|
| 263 | */
|
|---|
| 264 | public static Provider getProvider(String name)
|
|---|
| 265 | {
|
|---|
| 266 | Provider p = null;
|
|---|
| 267 | int max = providers.size ();
|
|---|
| 268 | for (int i = 0; i < max; i++)
|
|---|
| 269 | {
|
|---|
| 270 | p = (Provider) providers.elementAt(i);
|
|---|
| 271 | if (p.getName() == name)
|
|---|
| 272 | break;
|
|---|
| 273 | }
|
|---|
| 274 | return p;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /**
|
|---|
| 278 | Gets the value of a security property.
|
|---|
| 279 |
|
|---|
| 280 | This method checks the security manager with the call checkSecurityAccess
|
|---|
| 281 | with "getProperty."+key to see if the user can get this property.
|
|---|
| 282 |
|
|---|
| 283 | @param key property to get
|
|---|
| 284 |
|
|---|
| 285 | @return value of the property
|
|---|
| 286 |
|
|---|
| 287 | @throws SecurityException - if the security manager denies access to
|
|---|
| 288 | getting a property
|
|---|
| 289 | */
|
|---|
| 290 | public static String getProperty(String key)
|
|---|
| 291 | {
|
|---|
| 292 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 293 | if (sm != null)
|
|---|
| 294 | sm.checkSecurityAccess("getProperty." + key);
|
|---|
| 295 |
|
|---|
| 296 | return secprops.getProperty(key);
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 | /**
|
|---|
| 301 | Sets the value of a security property.
|
|---|
| 302 |
|
|---|
| 303 | This method checks the security manager with the call checkSecurityAccess
|
|---|
| 304 | with "setProperty."+key to see if the user can get this property.
|
|---|
| 305 |
|
|---|
| 306 | @param key property to set
|
|---|
| 307 | @param datnum new value of property
|
|---|
| 308 |
|
|---|
| 309 | @throws SecurityException - if the security manager denies access to
|
|---|
| 310 | setting a property
|
|---|
| 311 | */
|
|---|
| 312 | public static void setProperty(String key, String datnum)
|
|---|
| 313 | {
|
|---|
| 314 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 315 | if (sm != null)
|
|---|
| 316 | sm.checkSecurityAccess("setProperty." + key);
|
|---|
| 317 |
|
|---|
| 318 | secprops.put(key, datnum);
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|