Properties.java 12.4 KB
Newer Older
1
/* Properties.java -- run-time configuration properties.
2
   Copyright (C) 2003, 2004, 2006, 2010  Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

This file is a part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.  */


package gnu.java.security;

41 42
import gnu.java.security.Configuration;

43 44 45 46 47 48
import java.io.FileInputStream;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.PropertyPermission;
49
import java.util.logging.Logger;
50 51

/**
52 53
 * A global object containing build-specific properties that affect the
 * behaviour of the generated binaries from this library.
54 55 56
 */
public final class Properties
{
57 58
  private static final Logger log = Configuration.DEBUG ?
                        Logger.getLogger(Properties.class.getName()) : null;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  public static final String VERSION = "gnu.crypto.version";

  public static final String PROPERTIES_FILE = "gnu.crypto.properties.file";

  public static final String REPRODUCIBLE_PRNG = "gnu.crypto.with.reproducible.prng";

  public static final String CHECK_WEAK_KEYS = "gnu.crypto.with.check.for.weak.keys";

  public static final String DO_RSA_BLINDING = "gnu.crypto.with.rsa.blinding";

  private static final String TRUE = Boolean.TRUE.toString();

  private static final String FALSE = Boolean.FALSE.toString();

  private static final HashMap props = new HashMap();

  private static Properties singleton = null;

  private boolean reproducible = false;

  private boolean checkForWeakKeys = true;

  private boolean doRSABlinding = true;

  /** Trivial constructor to enforce Singleton pattern. */
  private Properties()
  {
    super();
    init();
  }

  /**
92 93
   * Returns the string representation of the library global configuration
   * property with the designated <code>key</code>.
94
   *
95
   * @param key the case-insensitive, non-null and non-empty name of a
96
   *          configuration property.
97
   * @return the string representation of the designated property, or
98 99
   *         <code>null</code> if such property is not yet set, or
   *         <code>key</code> is empty.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
   */
  public static final synchronized String getProperty(String key)
  {
    if (key == null)
      return null;
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(key, "read"));
    key = key.trim().toLowerCase();
    if ("".equals(key))
      return null;
    return (String) props.get(key);
  }

  /**
115 116
   * Sets the value of a designated library global configuration property, to a
   * string representation of what should be a legal value.
117
   *
118
   * @param key the case-insensitive, non-null and non-empty name of a
119 120 121
   *          configuration property.
   * @param value the non-null, non-empty string representation of a legal value
   *          of the configuration property named by <code>key</code>.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
   */
  public static final synchronized void setProperty(String key, String value)
  {
    if (key == null || value == null)
      return;
    key = key.trim().toLowerCase();
    if ("".equals(key))
      return;
    if (key.equals(VERSION))
      return;
    value = value.trim();
    if ("".equals(value))
      return;
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(key, "write"));
    if (key.equals(REPRODUCIBLE_PRNG)
        && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
      setReproducible(Boolean.valueOf(value).booleanValue());
    else if (key.equals(CHECK_WEAK_KEYS)
             && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
      setCheckForWeakKeys(Boolean.valueOf(value).booleanValue());
    else if (key.equals(DO_RSA_BLINDING)
             && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
      setDoRSABlinding(Boolean.valueOf(value).booleanValue());
    else
      props.put(key, value);
  }

  /**
152
   * A convenience method that returns, as a boolean, the library global
153
   * configuration property indicating if the default Pseudo Random Number
154
   * Generator produces, or not, the same bit stream when instantiated.
155
   *
156 157 158 159
   * @return <code>true</code> if the default PRNG produces the same bit
   *         stream with every VM instance. Returns <code>false</code> if the
   *         default PRNG is seeded with the time of day of its first
   *         invocation.
160 161 162 163 164 165 166 167 168 169
   */
  public static final synchronized boolean isReproducible()
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(REPRODUCIBLE_PRNG, "read"));
    return instance().reproducible;
  }

  /**
170 171 172 173 174
   * A convenience method that returns, as a boolean, the library global
   * configuration property indicating if the implementations of symmetric key
   * block ciphers check, or not, for possible/potential weak and semi-weak keys
   * that may be produced in the course of generating round encryption and/or
   * decryption keys.
175
   *
176 177 178
   * @return <code>true</code> if the cipher implementations check for weak
   *         and semi-weak keys. Returns <code>false</code> if the cipher
   *         implementations do not check for weak or semi-weak keys.
179 180 181 182 183 184 185 186 187 188
   */
  public static final synchronized boolean checkForWeakKeys()
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(CHECK_WEAK_KEYS, "read"));
    return instance().checkForWeakKeys;
  }

  /**
189
   * A convenience method that returns, as a boolean, the library global
190
   * configuration property indicating if RSA decryption (RSADP primitive),
191
   * does, or not, blinding against timing attacks.
192
   *
193
   * @return <code>true</code> if the RSA decryption primitive includes a
194 195 196
   *         blinding operation. Returns <code>false</code> if the RSA
   *         decryption primitive does not include the additional blinding
   *         operation.
197 198 199 200 201 202 203 204 205 206
   */
  public static final synchronized boolean doRSABlinding()
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(DO_RSA_BLINDING, "read"));
    return instance().doRSABlinding;
  }

  /**
207 208
   * A convenience method to set the global property for reproducibility of the
   * default PRNG bit stream output.
209
   *
210
   * @param value if <code>true</code> then the default PRNG bit stream output
211
   *          is the same with every invocation of the VM.
212 213 214 215 216 217 218 219 220 221 222
   */
  public static final synchronized void setReproducible(final boolean value)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(REPRODUCIBLE_PRNG, "write"));
    instance().reproducible = value;
    props.put(REPRODUCIBLE_PRNG, String.valueOf(value));
  }

  /**
223 224
   * A convenience method to set the global property for checking for weak and
   * semi-weak cipher keys.
225
   *
226
   * @param value if <code>true</code> then the cipher implementations will
227 228
   *          invoke additional checks for weak and semi-weak key values that
   *          may get generated.
229 230 231 232 233 234 235 236 237 238 239
   */
  public static final synchronized void setCheckForWeakKeys(final boolean value)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(CHECK_WEAK_KEYS, "write"));
    instance().checkForWeakKeys = value;
    props.put(CHECK_WEAK_KEYS, String.valueOf(value));
  }

  /**
240 241
   * A convenience method to set the global property fo adding a blinding
   * operation when executing the RSA decryption primitive.
242
   *
243
   * @param value if <code>true</code> then the code for performing the RSA
244
   *          decryption primitive will include a blinding operation.
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
   */
  public static final synchronized void setDoRSABlinding(final boolean value)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null)
      sm.checkPermission(new PropertyPermission(DO_RSA_BLINDING, "write"));
    instance().doRSABlinding = value;
    props.put(DO_RSA_BLINDING, String.valueOf(value));
  }

  private static final synchronized Properties instance()
  {
    if (singleton == null)
      singleton = new Properties();
    return singleton;
  }

  private void init()
  {
    // default values
    props.put(REPRODUCIBLE_PRNG, (reproducible ? "true" : "false"));
    props.put(CHECK_WEAK_KEYS, (checkForWeakKeys ? "true" : "false"));
    props.put(DO_RSA_BLINDING, (doRSABlinding ? "true" : "false"));
    // 1. allow site-wide override by reading a properties file
    String propFile = null;
    try
      {
        propFile = (String) AccessController.doPrivileged(new PrivilegedAction()
        {
          public Object run()
          {
            return System.getProperty(PROPERTIES_FILE);
          }
        });
      }
    catch (SecurityException se)
      {
282 283
        if (Configuration.DEBUG)
          log.fine("Reading property " + PROPERTIES_FILE + " not allowed. Ignored.");
284 285 286 287 288 289 290 291 292 293 294 295 296
      }
    if (propFile != null)
      {
        try
          {
            final java.util.Properties temp = new java.util.Properties();
            final FileInputStream fin = new FileInputStream(propFile);
            temp.load(fin);
            temp.list(System.out);
            props.putAll(temp);
          }
        catch (IOException ioe)
          {
297 298
            if (Configuration.DEBUG)
              log.fine("IO error reading " + propFile + ": " + ioe.getMessage());
299 300 301
          }
        catch (SecurityException se)
          {
302 303 304
            if (Configuration.DEBUG)
              log.fine("Security error reading " + propFile + ": "
                       + se.getMessage());
305 306 307 308 309 310 311
          }
      }
    // 2. allow vm-specific override by allowing -D options in launcher
    handleBooleanProperty(REPRODUCIBLE_PRNG);
    handleBooleanProperty(CHECK_WEAK_KEYS);
    handleBooleanProperty(DO_RSA_BLINDING);
    // re-sync the 'known' properties
312 313 314
    reproducible = Boolean.valueOf((String) props.get(REPRODUCIBLE_PRNG)).booleanValue();
    checkForWeakKeys = Boolean.valueOf((String) props.get(CHECK_WEAK_KEYS)).booleanValue();
    doRSABlinding = Boolean.valueOf((String) props.get(DO_RSA_BLINDING)).booleanValue();
315 316 317 318 319 320 321 322 323 324 325 326 327
    // This does not change.
    props.put(VERSION, Registry.VERSION_STRING);
  }

  private void handleBooleanProperty(final String name)
  {
    String s = null;
    try
      {
        s = System.getProperty(name);
      }
    catch (SecurityException x)
      {
328 329
        if (Configuration.DEBUG)
          log.fine("SecurityManager forbids reading system properties. Ignored");
330 331 332 333
      }
    if (s != null)
      {
        s = s.trim().toLowerCase();
334
        // we have to test for explicit "true" or "false". anything else may
335 336 337
        // hide valid value set previously
        if (s.equals(TRUE) || s.equals(FALSE))
          {
338 339
            if (Configuration.DEBUG)
              log.fine("Setting " + name + " to '" + s + "'");
340 341 342 343
            props.put(name, s);
          }
        else
          {
344 345
            if (Configuration.DEBUG)
              log.fine("Invalid value for -D" + name + ": " + s + ". Ignored");
346 347 348 349
          }
      }
  }
}