Source for gnu.java.security.Properties

   1: /* Properties.java -- run-time configuration properties.
   2:    Copyright (C) 2003, 2004, 2006, 2010  Free Software Foundation, Inc.
   3: 
   4: This file is a 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 of the License, or (at
   9: your option) 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; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: 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: 
  39: package gnu.java.security;
  40: 
  41: import gnu.java.security.Configuration;
  42: 
  43: import java.io.FileInputStream;
  44: import java.io.IOException;
  45: import java.security.AccessController;
  46: import java.security.PrivilegedAction;
  47: import java.util.HashMap;
  48: import java.util.PropertyPermission;
  49: import java.util.logging.Logger;
  50: 
  51: /**
  52:  * A global object containing build-specific properties that affect the
  53:  * behaviour of the generated binaries from this library.
  54:  */
  55: public final class Properties
  56: {
  57:   private static final Logger log = Configuration.DEBUG ?
  58:                         Logger.getLogger(Properties.class.getName()) : null;
  59: 
  60:   public static final String VERSION = "gnu.crypto.version";
  61: 
  62:   public static final String PROPERTIES_FILE = "gnu.crypto.properties.file";
  63: 
  64:   public static final String REPRODUCIBLE_PRNG = "gnu.crypto.with.reproducible.prng";
  65: 
  66:   public static final String CHECK_WEAK_KEYS = "gnu.crypto.with.check.for.weak.keys";
  67: 
  68:   public static final String DO_RSA_BLINDING = "gnu.crypto.with.rsa.blinding";
  69: 
  70:   private static final String TRUE = Boolean.TRUE.toString();
  71: 
  72:   private static final String FALSE = Boolean.FALSE.toString();
  73: 
  74:   private static final HashMap props = new HashMap();
  75: 
  76:   private static Properties singleton = null;
  77: 
  78:   private boolean reproducible = false;
  79: 
  80:   private boolean checkForWeakKeys = true;
  81: 
  82:   private boolean doRSABlinding = true;
  83: 
  84:   /** Trivial constructor to enforce Singleton pattern. */
  85:   private Properties()
  86:   {
  87:     super();
  88:     init();
  89:   }
  90: 
  91:   /**
  92:    * Returns the string representation of the library global configuration
  93:    * 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:    *         <code>null</code> if such property is not yet set, or
  99:    *         <code>key</code> is empty.
 100:    */
 101:   public static final synchronized String getProperty(String key)
 102:   {
 103:     if (key == null)
 104:       return null;
 105:     SecurityManager sm = System.getSecurityManager();
 106:     if (sm != null)
 107:       sm.checkPermission(new PropertyPermission(key, "read"));
 108:     key = key.trim().toLowerCase();
 109:     if ("".equals(key))
 110:       return null;
 111:     return (String) props.get(key);
 112:   }
 113: 
 114:   /**
 115:    * Sets the value of a designated library global configuration property, to a
 116:    * 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:    *          configuration property.
 120:    * @param value the non-null, non-empty string representation of a legal value
 121:    *          of the configuration property named by <code>key</code>.
 122:    */
 123:   public static final synchronized void setProperty(String key, String value)
 124:   {
 125:     if (key == null || value == null)
 126:       return;
 127:     key = key.trim().toLowerCase();
 128:     if ("".equals(key))
 129:       return;
 130:     if (key.equals(VERSION))
 131:       return;
 132:     value = value.trim();
 133:     if ("".equals(value))
 134:       return;
 135:     SecurityManager sm = System.getSecurityManager();
 136:     if (sm != null)
 137:       sm.checkPermission(new PropertyPermission(key, "write"));
 138:     if (key.equals(REPRODUCIBLE_PRNG)
 139:         && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
 140:       setReproducible(Boolean.valueOf(value).booleanValue());
 141:     else if (key.equals(CHECK_WEAK_KEYS)
 142:              && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
 143:       setCheckForWeakKeys(Boolean.valueOf(value).booleanValue());
 144:     else if (key.equals(DO_RSA_BLINDING)
 145:              && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
 146:       setDoRSABlinding(Boolean.valueOf(value).booleanValue());
 147:     else
 148:       props.put(key, value);
 149:   }
 150: 
 151:   /**
 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:    * @return <code>true</code> if the default PRNG produces the same bit
 157:    *         stream with every VM instance. Returns <code>false</code> if the
 158:    *         default PRNG is seeded with the time of day of its first
 159:    *         invocation.
 160:    */
 161:   public static final synchronized boolean isReproducible()
 162:   {
 163:     SecurityManager sm = System.getSecurityManager();
 164:     if (sm != null)
 165:       sm.checkPermission(new PropertyPermission(REPRODUCIBLE_PRNG, "read"));
 166:     return instance().reproducible;
 167:   }
 168: 
 169:   /**
 170:    * A convenience method that returns, as a boolean, the library global
 171:    * configuration property indicating if the implementations of symmetric key
 172:    * block ciphers check, or not, for possible/potential weak and semi-weak keys
 173:    * that may be produced in the course of generating round encryption and/or
 174:    * decryption keys.
 175:    *
 176:    * @return <code>true</code> if the cipher implementations check for weak
 177:    *         and semi-weak keys. Returns <code>false</code> if the cipher
 178:    *         implementations do not check for weak or semi-weak keys.
 179:    */
 180:   public static final synchronized boolean checkForWeakKeys()
 181:   {
 182:     SecurityManager sm = System.getSecurityManager();
 183:     if (sm != null)
 184:       sm.checkPermission(new PropertyPermission(CHECK_WEAK_KEYS, "read"));
 185:     return instance().checkForWeakKeys;
 186:   }
 187: 
 188:   /**
 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:    *         blinding operation. Returns <code>false</code> if the RSA
 195:    *         decryption primitive does not include the additional blinding
 196:    *         operation.
 197:    */
 198:   public static final synchronized boolean doRSABlinding()
 199:   {
 200:     SecurityManager sm = System.getSecurityManager();
 201:     if (sm != null)
 202:       sm.checkPermission(new PropertyPermission(DO_RSA_BLINDING, "read"));
 203:     return instance().doRSABlinding;
 204:   }
 205: 
 206:   /**
 207:    * A convenience method to set the global property for reproducibility of the
 208:    * 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:   public static final synchronized void setReproducible(final boolean value)
 214:   {
 215:     SecurityManager sm = System.getSecurityManager();
 216:     if (sm != null)
 217:       sm.checkPermission(new PropertyPermission(REPRODUCIBLE_PRNG, "write"));
 218:     instance().reproducible = value;
 219:     props.put(REPRODUCIBLE_PRNG, String.valueOf(value));
 220:   }
 221: 
 222:   /**
 223:    * A convenience method to set the global property for checking for weak and
 224:    * semi-weak cipher keys.
 225:    *
 226:    * @param value if <code>true</code> then the cipher implementations will
 227:    *          invoke additional checks for weak and semi-weak key values that
 228:    *          may get generated.
 229:    */
 230:   public static final synchronized void setCheckForWeakKeys(final boolean value)
 231:   {
 232:     SecurityManager sm = System.getSecurityManager();
 233:     if (sm != null)
 234:       sm.checkPermission(new PropertyPermission(CHECK_WEAK_KEYS, "write"));
 235:     instance().checkForWeakKeys = value;
 236:     props.put(CHECK_WEAK_KEYS, String.valueOf(value));
 237:   }
 238: 
 239:   /**
 240:    * A convenience method to set the global property fo adding a blinding
 241:    * 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:   public static final synchronized void setDoRSABlinding(final boolean value)
 247:   {
 248:     SecurityManager sm = System.getSecurityManager();
 249:     if (sm != null)
 250:       sm.checkPermission(new PropertyPermission(DO_RSA_BLINDING, "write"));
 251:     instance().doRSABlinding = value;
 252:     props.put(DO_RSA_BLINDING, String.valueOf(value));
 253:   }
 254: 
 255:   private static final synchronized Properties instance()
 256:   {
 257:     if (singleton == null)
 258:       singleton = new Properties();
 259:     return singleton;
 260:   }
 261: 
 262:   private void init()
 263:   {
 264:     // default values
 265:     props.put(REPRODUCIBLE_PRNG, (reproducible ? "true" : "false"));
 266:     props.put(CHECK_WEAK_KEYS, (checkForWeakKeys ? "true" : "false"));
 267:     props.put(DO_RSA_BLINDING, (doRSABlinding ? "true" : "false"));
 268:     // 1. allow site-wide override by reading a properties file
 269:     String propFile = null;
 270:     try
 271:       {
 272:         propFile = (String) AccessController.doPrivileged(new PrivilegedAction()
 273:         {
 274:           public Object run()
 275:           {
 276:             return System.getProperty(PROPERTIES_FILE);
 277:           }
 278:         });
 279:       }
 280:     catch (SecurityException se)
 281:       {
 282:         if (Configuration.DEBUG)
 283:           log.fine("Reading property " + PROPERTIES_FILE + " not allowed. Ignored.");
 284:       }
 285:     if (propFile != null)
 286:       {
 287:         try
 288:           {
 289:             final java.util.Properties temp = new java.util.Properties();
 290:             final FileInputStream fin = new FileInputStream(propFile);
 291:             temp.load(fin);
 292:             temp.list(System.out);
 293:             props.putAll(temp);
 294:           }
 295:         catch (IOException ioe)
 296:           {
 297:             if (Configuration.DEBUG)
 298:               log.fine("IO error reading " + propFile + ": " + ioe.getMessage());
 299:           }
 300:         catch (SecurityException se)
 301:           {
 302:             if (Configuration.DEBUG)
 303:               log.fine("Security error reading " + propFile + ": "
 304:                        + se.getMessage());
 305:           }
 306:       }
 307:     // 2. allow vm-specific override by allowing -D options in launcher
 308:     handleBooleanProperty(REPRODUCIBLE_PRNG);
 309:     handleBooleanProperty(CHECK_WEAK_KEYS);
 310:     handleBooleanProperty(DO_RSA_BLINDING);
 311:     // re-sync the 'known' properties
 312:     reproducible = Boolean.valueOf((String) props.get(REPRODUCIBLE_PRNG)).booleanValue();
 313:     checkForWeakKeys = Boolean.valueOf((String) props.get(CHECK_WEAK_KEYS)).booleanValue();
 314:     doRSABlinding = Boolean.valueOf((String) props.get(DO_RSA_BLINDING)).booleanValue();
 315:     // This does not change.
 316:     props.put(VERSION, Registry.VERSION_STRING);
 317:   }
 318: 
 319:   private void handleBooleanProperty(final String name)
 320:   {
 321:     String s = null;
 322:     try
 323:       {
 324:         s = System.getProperty(name);
 325:       }
 326:     catch (SecurityException x)
 327:       {
 328:         if (Configuration.DEBUG)
 329:           log.fine("SecurityManager forbids reading system properties. Ignored");
 330:       }
 331:     if (s != null)
 332:       {
 333:         s = s.trim().toLowerCase();
 334:         // we have to test for explicit "true" or "false". anything else may
 335:         // hide valid value set previously
 336:         if (s.equals(TRUE) || s.equals(FALSE))
 337:           {
 338:             if (Configuration.DEBUG)
 339:               log.fine("Setting " + name + " to '" + s + "'");
 340:             props.put(name, s);
 341:           }
 342:         else
 343:           {
 344:             if (Configuration.DEBUG)
 345:               log.fine("Invalid value for -D" + name + ": " + s + ". Ignored");
 346:           }
 347:       }
 348:   }
 349: }