1 |
769 |
jeremybenn |
/* Properties.java -- run-time configuration properties.
|
2 |
|
|
Copyright (C) 2003, 2004, 2006 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 = 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 |
|
|
/** Trivial constructor to enforce Singleton pattern. */
|
84 |
|
|
private Properties()
|
85 |
|
|
{
|
86 |
|
|
super();
|
87 |
|
|
init();
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* Returns the string representation of the library global configuration
|
92 |
|
|
* property with the designated <code>key</code>.
|
93 |
|
|
*
|
94 |
|
|
* @param key the case-insensitive, non-null and non-empty name of a
|
95 |
|
|
* configuration property.
|
96 |
|
|
* @return the string representation of the designated property, or
|
97 |
|
|
* <code>null</code> if such property is not yet set, or
|
98 |
|
|
* <code>key</code> is empty.
|
99 |
|
|
*/
|
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 |
|
|
/**
|
114 |
|
|
* Sets the value of a designated library global configuration property, to a
|
115 |
|
|
* string representation of what should be a legal value.
|
116 |
|
|
*
|
117 |
|
|
* @param key the case-insensitive, non-null and non-empty name of a
|
118 |
|
|
* configuration property.
|
119 |
|
|
* @param value the non-null, non-empty string representation of a legal value
|
120 |
|
|
* of the configuration property named by <code>key</code>.
|
121 |
|
|
*/
|
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 |
|
|
/**
|
151 |
|
|
* A convenience method that returns, as a boolean, the library global
|
152 |
|
|
* configuration property indicating if the default Pseudo Random Number
|
153 |
|
|
* Generator produces, or not, the same bit stream when instantiated.
|
154 |
|
|
*
|
155 |
|
|
* @return <code>true</code> if the default PRNG produces the same bit
|
156 |
|
|
* stream with every VM instance. Returns <code>false</code> if the
|
157 |
|
|
* default PRNG is seeded with the time of day of its first
|
158 |
|
|
* invocation.
|
159 |
|
|
*/
|
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 |
|
|
/**
|
169 |
|
|
* A convenience method that returns, as a boolean, the library global
|
170 |
|
|
* configuration property indicating if the implementations of symmetric key
|
171 |
|
|
* block ciphers check, or not, for possible/potential weak and semi-weak keys
|
172 |
|
|
* that may be produced in the course of generating round encryption and/or
|
173 |
|
|
* decryption keys.
|
174 |
|
|
*
|
175 |
|
|
* @return <code>true</code> if the cipher implementations check for weak
|
176 |
|
|
* and semi-weak keys. Returns <code>false</code> if the cipher
|
177 |
|
|
* implementations do not check for weak or semi-weak keys.
|
178 |
|
|
*/
|
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 |
|
|
/**
|
188 |
|
|
* A convenience method that returns, as a boolean, the library global
|
189 |
|
|
* configuration property indicating if RSA decryption (RSADP primitive),
|
190 |
|
|
* does, or not, blinding against timing attacks.
|
191 |
|
|
*
|
192 |
|
|
* @return <code>true</code> if the RSA decryption primitive includes a
|
193 |
|
|
* blinding operation. Returns <code>false</code> if the RSA
|
194 |
|
|
* decryption primitive does not include the additional blinding
|
195 |
|
|
* operation.
|
196 |
|
|
*/
|
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 |
|
|
/**
|
206 |
|
|
* A convenience method to set the global property for reproducibility of the
|
207 |
|
|
* default PRNG bit stream output.
|
208 |
|
|
*
|
209 |
|
|
* @param value if <code>true</code> then the default PRNG bit stream output
|
210 |
|
|
* is the same with every invocation of the VM.
|
211 |
|
|
*/
|
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 |
|
|
/**
|
222 |
|
|
* A convenience method to set the global property for checking for weak and
|
223 |
|
|
* semi-weak cipher keys.
|
224 |
|
|
*
|
225 |
|
|
* @param value if <code>true</code> then the cipher implementations will
|
226 |
|
|
* invoke additional checks for weak and semi-weak key values that
|
227 |
|
|
* may get generated.
|
228 |
|
|
*/
|
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 |
|
|
/**
|
239 |
|
|
* A convenience method to set the global property fo adding a blinding
|
240 |
|
|
* operation when executing the RSA decryption primitive.
|
241 |
|
|
*
|
242 |
|
|
* @param value if <code>true</code> then the code for performing the RSA
|
243 |
|
|
* decryption primitive will include a blinding operation.
|
244 |
|
|
*/
|
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 |
|
|
// default values
|
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 |
|
|
// 1. allow site-wide override by reading a properties file
|
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 |
|
|
// 2. allow vm-specific override by allowing -D options in launcher
|
307 |
|
|
handleBooleanProperty(REPRODUCIBLE_PRNG);
|
308 |
|
|
handleBooleanProperty(CHECK_WEAK_KEYS);
|
309 |
|
|
handleBooleanProperty(DO_RSA_BLINDING);
|
310 |
|
|
// re-sync the 'known' properties
|
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 |
|
|
// This does not change.
|
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 |
|
|
// we have to test for explicit "true" or "false". anything else may
|
334 |
|
|
// hide valid value set previously
|
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 |
|
|
}
|