1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50:
51:
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:
85: private Properties()
86: {
87: super();
88: init();
89: }
90:
91:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
308: handleBooleanProperty(REPRODUCIBLE_PRNG);
309: handleBooleanProperty(CHECK_WEAK_KEYS);
310: handleBooleanProperty(DO_RSA_BLINDING);
311:
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:
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:
335:
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: }