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 = Logger.getLogger(Properties.class.getName());
58:
59: public static final String VERSION = "gnu.crypto.version";
60:
61: public static final String PROPERTIES_FILE = "gnu.crypto.properties.file";
62:
63: public static final String REPRODUCIBLE_PRNG = "gnu.crypto.with.reproducible.prng";
64:
65: public static final String CHECK_WEAK_KEYS = "gnu.crypto.with.check.for.weak.keys";
66:
67: public static final String DO_RSA_BLINDING = "gnu.crypto.with.rsa.blinding";
68:
69: private static final String TRUE = Boolean.TRUE.toString();
70:
71: private static final String FALSE = Boolean.FALSE.toString();
72:
73: private static final HashMap props = new HashMap();
74:
75: private static Properties singleton = null;
76:
77: private boolean reproducible = false;
78:
79: private boolean checkForWeakKeys = true;
80:
81: private boolean doRSABlinding = true;
82:
83:
84: private Properties()
85: {
86: super();
87: init();
88: }
89:
90:
100: public static final synchronized String getProperty(String key)
101: {
102: if (key == null)
103: return null;
104: SecurityManager sm = System.getSecurityManager();
105: if (sm != null)
106: sm.checkPermission(new PropertyPermission(key, "read"));
107: key = key.trim().toLowerCase();
108: if ("".equals(key))
109: return null;
110: return (String) props.get(key);
111: }
112:
113:
122: public static final synchronized void setProperty(String key, String value)
123: {
124: if (key == null || value == null)
125: return;
126: key = key.trim().toLowerCase();
127: if ("".equals(key))
128: return;
129: if (key.equals(VERSION))
130: return;
131: value = value.trim();
132: if ("".equals(value))
133: return;
134: SecurityManager sm = System.getSecurityManager();
135: if (sm != null)
136: sm.checkPermission(new PropertyPermission(key, "write"));
137: if (key.equals(REPRODUCIBLE_PRNG)
138: && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
139: setReproducible(Boolean.valueOf(value).booleanValue());
140: else if (key.equals(CHECK_WEAK_KEYS)
141: && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
142: setCheckForWeakKeys(Boolean.valueOf(value).booleanValue());
143: else if (key.equals(DO_RSA_BLINDING)
144: && (value.equalsIgnoreCase(TRUE) || value.equalsIgnoreCase(FALSE)))
145: setDoRSABlinding(Boolean.valueOf(value).booleanValue());
146: else
147: props.put(key, value);
148: }
149:
150:
160: public static final synchronized boolean isReproducible()
161: {
162: SecurityManager sm = System.getSecurityManager();
163: if (sm != null)
164: sm.checkPermission(new PropertyPermission(REPRODUCIBLE_PRNG, "read"));
165: return instance().reproducible;
166: }
167:
168:
179: public static final synchronized boolean checkForWeakKeys()
180: {
181: SecurityManager sm = System.getSecurityManager();
182: if (sm != null)
183: sm.checkPermission(new PropertyPermission(CHECK_WEAK_KEYS, "read"));
184: return instance().checkForWeakKeys;
185: }
186:
187:
197: public static final synchronized boolean doRSABlinding()
198: {
199: SecurityManager sm = System.getSecurityManager();
200: if (sm != null)
201: sm.checkPermission(new PropertyPermission(DO_RSA_BLINDING, "read"));
202: return instance().doRSABlinding;
203: }
204:
205:
212: public static final synchronized void setReproducible(final boolean value)
213: {
214: SecurityManager sm = System.getSecurityManager();
215: if (sm != null)
216: sm.checkPermission(new PropertyPermission(REPRODUCIBLE_PRNG, "write"));
217: instance().reproducible = value;
218: props.put(REPRODUCIBLE_PRNG, String.valueOf(value));
219: }
220:
221:
229: public static final synchronized void setCheckForWeakKeys(final boolean value)
230: {
231: SecurityManager sm = System.getSecurityManager();
232: if (sm != null)
233: sm.checkPermission(new PropertyPermission(CHECK_WEAK_KEYS, "write"));
234: instance().checkForWeakKeys = value;
235: props.put(CHECK_WEAK_KEYS, String.valueOf(value));
236: }
237:
238:
245: public static final synchronized void setDoRSABlinding(final boolean value)
246: {
247: SecurityManager sm = System.getSecurityManager();
248: if (sm != null)
249: sm.checkPermission(new PropertyPermission(DO_RSA_BLINDING, "write"));
250: instance().doRSABlinding = value;
251: props.put(DO_RSA_BLINDING, String.valueOf(value));
252: }
253:
254: private static final synchronized Properties instance()
255: {
256: if (singleton == null)
257: singleton = new Properties();
258: return singleton;
259: }
260:
261: private void init()
262: {
263:
264: props.put(REPRODUCIBLE_PRNG, (reproducible ? "true" : "false"));
265: props.put(CHECK_WEAK_KEYS, (checkForWeakKeys ? "true" : "false"));
266: props.put(DO_RSA_BLINDING, (doRSABlinding ? "true" : "false"));
267:
268: String propFile = null;
269: try
270: {
271: propFile = (String) AccessController.doPrivileged(new PrivilegedAction()
272: {
273: public Object run()
274: {
275: return System.getProperty(PROPERTIES_FILE);
276: }
277: });
278: }
279: catch (SecurityException se)
280: {
281: if (Configuration.DEBUG)
282: log.fine("Reading property " + PROPERTIES_FILE + " not allowed. Ignored.");
283: }
284: if (propFile != null)
285: {
286: try
287: {
288: final java.util.Properties temp = new java.util.Properties();
289: final FileInputStream fin = new FileInputStream(propFile);
290: temp.load(fin);
291: temp.list(System.out);
292: props.putAll(temp);
293: }
294: catch (IOException ioe)
295: {
296: if (Configuration.DEBUG)
297: log.fine("IO error reading " + propFile + ": " + ioe.getMessage());
298: }
299: catch (SecurityException se)
300: {
301: if (Configuration.DEBUG)
302: log.fine("Security error reading " + propFile + ": "
303: + se.getMessage());
304: }
305: }
306:
307: handleBooleanProperty(REPRODUCIBLE_PRNG);
308: handleBooleanProperty(CHECK_WEAK_KEYS);
309: handleBooleanProperty(DO_RSA_BLINDING);
310:
311: reproducible = Boolean.valueOf((String) props.get(REPRODUCIBLE_PRNG)).booleanValue();
312: checkForWeakKeys = Boolean.valueOf((String) props.get(CHECK_WEAK_KEYS)).booleanValue();
313: doRSABlinding = Boolean.valueOf((String) props.get(DO_RSA_BLINDING)).booleanValue();
314:
315: props.put(VERSION, Registry.VERSION_STRING);
316: }
317:
318: private void handleBooleanProperty(final String name)
319: {
320: String s = null;
321: try
322: {
323: s = System.getProperty(name);
324: }
325: catch (SecurityException x)
326: {
327: if (Configuration.DEBUG)
328: log.fine("SecurityManager forbids reading system properties. Ignored");
329: }
330: if (s != null)
331: {
332: s = s.trim().toLowerCase();
333:
334:
335: if (s.equals(TRUE) || s.equals(FALSE))
336: {
337: if (Configuration.DEBUG)
338: log.fine("Setting " + name + " to '" + s + "'");
339: props.put(name, s);
340: }
341: else
342: {
343: if (Configuration.DEBUG)
344: log.fine("Invalid value for -D" + name + ": " + s + ". Ignored");
345: }
346: }
347: }
348: }