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