diff options
author | Casey Marshall <csm@gnu.org> | 2006-04-14 18:33:42 +0000 |
---|---|---|
committer | Casey Marshall <csm@gnu.org> | 2006-04-14 18:33:42 +0000 |
commit | 5d9e591b317b07c4e05ea062a7dfc7c79f64e9ea (patch) | |
tree | 2dcea6fe9ecb3c27c0db363fa880452192e4e0b6 /java/security | |
parent | 4484590e69caf8a33d097cb68d0d0875156f3c0a (diff) | |
download | classpath-5d9e591b317b07c4e05ea062a7dfc7c79f64e9ea.tar.gz |
2006-04-14 Casey Marshall <csm@gnu.org>
Fixes PR classpath/24642
* NEWS: add note about SecureRandom changes, and addition of
VMSecureRandom.
* java/security/SecureRandom.java (isSeeded): new field.
(setSeed, setSeed): set `isSeeded' to `true.'
(nextBytes): seed this instance if `isSeeded' is false.
(getSeed): call `generateSeed.'
(SECURERANDOM_SOURCE, JAVA_SECURITY_EGD, logger): new constants.
(generateSeed, generateSeed): new methods.
* vm/reference/java/security/VMSecureRandom.java: new file.
Diffstat (limited to 'java/security')
-rw-r--r-- | java/security/SecureRandom.java | 78 |
1 files changed, 75 insertions, 3 deletions
diff --git a/java/security/SecureRandom.java b/java/security/SecureRandom.java index 5ac9a4a8c..d403d4964 100644 --- a/java/security/SecureRandom.java +++ b/java/security/SecureRandom.java @@ -38,11 +38,19 @@ exception statement from your version. */ package java.security; +import gnu.classpath.SystemProperties; import gnu.java.security.Engine; +import gnu.java.security.action.GetSecurityPropertyAction; import gnu.java.security.jce.prng.Sha160RandomSpi; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Enumeration; import java.util.Random; +import java.util.logging.Level; +import java.util.logging.Logger; /** * An interface to a cryptographically secure pseudo-random number @@ -74,6 +82,8 @@ public class SecureRandom extends Random byte[] state = null; private String algorithm; + private boolean isSeeded = false; + // Constructors. // ------------------------------------------------------------------------ @@ -303,6 +313,7 @@ public class SecureRandom extends Random public void setSeed(byte[] seed) { secureRandomSpi.engineSetSeed(seed); + isSeeded = true; } /** @@ -330,6 +341,7 @@ public class SecureRandom extends Random (byte) (0xff & seed) }; secureRandomSpi.engineSetSeed(tmp); + isSeeded = true; } } @@ -341,6 +353,8 @@ public class SecureRandom extends Random */ public void nextBytes(byte[] bytes) { + if (!isSeeded) + setSeed(getSeed(32)); randomBytesUsed += bytes.length; counter++; secureRandomSpi.engineNextBytes(bytes); @@ -386,10 +400,8 @@ public class SecureRandom extends Random public static byte[] getSeed(int numBytes) { byte[] tmp = new byte[numBytes]; - - new Random().nextBytes(tmp); + generateSeed(tmp); return tmp; - //return secureRandomSpi.engineGenerateSeed( numBytes ); } /** @@ -404,4 +416,64 @@ public class SecureRandom extends Random return secureRandomSpi.engineGenerateSeed(numBytes); } + // Seed methods. + + private static final String SECURERANDOM_SOURCE = "securerandom.source"; + private static final String JAVA_SECURITY_EGD = "java.security.egd"; + private static final Logger logger = Logger.getLogger(SecureRandom.class.getName()); + + private static int generateSeed(byte[] buffer) + { + return generateSeed(buffer, 0, buffer.length); + } + + private static int generateSeed(byte[] buffer, int offset, int length) + { + URL sourceUrl = null; + String urlStr = null; + + GetSecurityPropertyAction action = new GetSecurityPropertyAction(SECURERANDOM_SOURCE); + try + { + urlStr = (String) AccessController.doPrivileged(action); + if (urlStr != null) + sourceUrl = new URL(urlStr); + } + catch (MalformedURLException ignored) + { + logger.log(Level.WARNING, SECURERANDOM_SOURCE + " property is malformed: {0}", + urlStr); + } + + if (sourceUrl == null) + { + try + { + urlStr = SystemProperties.getProperty(JAVA_SECURITY_EGD); + if (urlStr != null) + sourceUrl = new URL(urlStr); + } + catch (MalformedURLException mue) + { + logger.log(Level.WARNING, JAVA_SECURITY_EGD + " property is malformed: {0}", + urlStr); + } + } + + if (sourceUrl != null) + { + try + { + InputStream in = sourceUrl.openStream(); + return in.read(buffer, offset, length); + } + catch (IOException ioe) + { + logger.log(Level.FINE, "error reading random bytes", ioe); + } + } + + // If we get here, we did not get any seed from a property URL. + return VMSecureRandom.generateSeed(buffer, offset, length); + } } |