SecurityRandom (digest): Removed field.

2002-12-13  Casey Marshall  <rsdio@metastatic.org>
	    Mark Wielaard  <mark@klomp.org>

	* java/security/SecurityRandom (digest): Removed field.
	(SecureRandom): Check all providers for case-insensitive SecureRandom
	implementation. Don't ignore classname == null. Fallback to SHA1PRNG
	if necessary.
	(getInstance(String,Provider,boolean): New method.
	(getInstance(String)): Use new method.
	(getInstance(String,String)): Likewise.
	(getInstance(String,Provider)): Likewise.

2002-12-13  Casey Marshall  <rsdio@metastatic.org>

	* java/security/Security.java (loadProviders): Increment i only once.

Co-Authored-By: Mark Wielaard <mark@klomp.org>

From-SVN: r60099
This commit is contained in:
Casey Marshall 2002-12-13 14:21:07 +00:00 committed by Mark Wielaard
parent 697d8028c2
commit b4f145f326
3 changed files with 128 additions and 90 deletions

View File

@ -1,3 +1,19 @@
2002-12-13 Casey Marshall <rsdio@metastatic.org>
Mark Wielaard <mark@klomp.org>
* java/security/SecurityRandom (digest): Removed field.
(SecureRandom): Check all providers for case-insensitive SecureRandom
implementation. Don't ignore classname == null. Fallback to SHA1PRNG
if necessary.
(getInstance(String,Provider,boolean): New method.
(getInstance(String)): Use new method.
(getInstance(String,String)): Likewise.
(getInstance(String,Provider)): Likewise.
2002-12-13 Casey Marshall <rsdio@metastatic.org>
* java/security/Security.java (loadProviders): Increment i only once.
2002-12-12 Mark Wielaard <mark@klomp.org> 2002-12-12 Mark Wielaard <mark@klomp.org>
* java/lang/ClassLoader.java (resolveClass0): Transform * java/lang/ClassLoader.java (resolveClass0): Transform

View File

@ -1,5 +1,5 @@
/* SecureRandom.java --- Secure Random class implmentation /* SecureRandom.java --- Secure Random class implmentation
Copyright (C) 1999, 2001 Free Software Foundation, Inc. Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
@ -53,7 +53,6 @@ public class SecureRandom extends Random
//Serialized Field //Serialized Field
long counter = 0; //Serialized long counter = 0; //Serialized
MessageDigest digest = null;
Provider provider = null; Provider provider = null;
byte[] randomBytes = null; //Always null byte[] randomBytes = null; //Always null
int randomBytesUsed = 0; int randomBytesUsed = 0;
@ -87,37 +86,25 @@ public class SecureRandom extends Random
while (e.hasMoreElements()) while (e.hasMoreElements())
{ {
key = (String) e.nextElement(); key = (String) e.nextElement();
if (key.startsWith("SecureRandom.")) if (key.startsWith("SECURERANDOM."))
{
if ((classname = p[i].getProperty(key)) != null) if ((classname = p[i].getProperty(key)) != null)
break; {
}
if (classname != null)
break;
}
//if( classname == null)
// throw new NoSuchAlgorithmException();
try try
{ {
this.secureRandomSpi = secureRandomSpi = (SecureRandomSpi) Class.
(SecureRandomSpi) Class.forName(classname).newInstance(); forName(classname).newInstance();
provider = p[i];
return;
}
catch (Throwable ignore) { }
}
}
}
}
//s.algorithm = algorithm; // Nothing found. Fall back to SHA1PRNG
this.provider = p[i]; secureRandomSpi = new gnu.java.security.provider.SHA1PRNG();
}
catch (ClassNotFoundException cnfe)
{
//throw new NoSuchAlgorithmException("Class not found");
}
catch (InstantiationException ie)
{
//throw new NoSuchAlgorithmException("Class instantiation failed");
}
catch (IllegalAccessException iae)
{
//throw new NoSuchAlgorithmException("Illegal Access");
}
} }
/** /**
@ -167,40 +154,17 @@ public class SecureRandom extends Random
NoSuchAlgorithmException NoSuchAlgorithmException
{ {
Provider p[] = Security.getProviders(); Provider p[] = Security.getProviders();
for (int i = 0; i < p.length; i++)
//Format of Key: SecureRandom.algname
StringBuffer key = new StringBuffer("SecureRandom.");
key.append(algorithm);
String classname = null;
int i;
for (i = 0; i < p.length; i++)
{ {
if ((classname = p[i].getProperty(key.toString())) != null)
break;
}
if (classname == null)
throw new NoSuchAlgorithmException();
try try
{ {
return new SecureRandom((SecureRandomSpi) Class.forName(classname). return getInstance(algorithm, p[i]);
newInstance(), p[i]);
} }
catch (ClassNotFoundException cnfe) catch (NoSuchAlgorithmException ignored) { }
{
throw new NoSuchAlgorithmException("Class not found");
}
catch (InstantiationException ie)
{
throw new NoSuchAlgorithmException("Class instantiation failed");
}
catch (IllegalAccessException iae)
{
throw new NoSuchAlgorithmException("Illegal Access");
} }
// None found.
throw new NoSuchAlgorithmException(algorithm);
} }
/** /**
@ -223,32 +187,90 @@ public class SecureRandom extends Random
if (p == null) if (p == null)
throw new NoSuchProviderException(); throw new NoSuchProviderException();
//Format of Key: SecureRandom.algName return getInstance(algorithm, p);
StringBuffer key = new StringBuffer("SecureRandom."); }
key.append(algorithm);
String classname = p.getProperty(key.toString()); /**
if (classname == null) Returns an instance of a SecureRandom. It creates the class for
throw new NoSuchAlgorithmException(); the specified algorithm from the given provider.
@param algorithm The SecureRandom algorithm to create.
@param provider The provider to get the instance from.
@throws NoSuchAlgorithmException If the algorithm cannot be found, or
if the class cannot be instantiated.
*/
public static SecureRandom getInstance(String algorithm,
Provider provider) throws
NoSuchAlgorithmException
{
return getInstance(algorithm, provider, true);
}
/**
Creates the instance of SecureRandom, recursing to resolve aliases.
@param algorithm The SecureRandom algorithm to create.
@param provider The provider to get the implementation from.
@param recurse Whether or not to recurse to resolve aliases.
@throws NoSuchAlgorithmException If the algorithm cannot be found,
if there are too many aliases, or if the class cannot be
instantiated.
*/
private static SecureRandom getInstance(String algorithm,
Provider provider,
boolean recurse)
throws NoSuchAlgorithmException
{
String msg = algorithm;
for (Enumeration e = provider.propertyNames(); e.hasMoreElements(); )
{
// We could replace the boolean with an integer, incrementing it
// every
String key = (String) e.nextElement();
if (key.startsWith("SECURERANDOM.")
&& key.substring(13).equalsIgnoreCase(algorithm))
{
try try
{ {
return new SecureRandom((SecureRandomSpi) Class.forName(classname). Class c = Class.forName(provider.getProperty(key));
newInstance(), p); return new SecureRandom((SecureRandomSpi) c.newInstance(),
provider);
} }
catch (ClassNotFoundException cnfe) catch (Throwable ignored) { }
}
else if (key.startsWith("ALG.ALIAS.SECURERANDOM.")
&& key.substring(23).equalsIgnoreCase(algorithm) && recurse)
{ {
throw new NoSuchAlgorithmException("Class not found"); try
}
catch (InstantiationException ie)
{ {
throw new NoSuchAlgorithmException("Class instantiation failed"); // First see if this alias refers to a class in this
// provider.
return getInstance(provider.getProperty(key), provider, false);
} }
catch (IllegalAccessException iae) catch (NoSuchAlgorithmException nsae)
{ {
throw new NoSuchAlgorithmException("Illegal Access"); Provider[] provs = Security.getProviders();
for (int i = 0; i < provs.length; i++)
{
if (provs[i] == provider)
continue;
// Now try other providers for the implementation
try
{
return getInstance(provider.getProperty(key),
provs[i], false);
} }
catch (NoSuchAlgorithmException nsae2)
{
msg = nsae2.getMessage();
}
}
}
}
}
throw new NoSuchAlgorithmException(algorithm);
} }
/** /**

View File

@ -89,7 +89,7 @@ public final class Security extends Object
int i = 1; int i = 1;
String name; String name;
while ((name = secprops.getProperty("security.provider." + i++)) != while ((name = secprops.getProperty("security.provider." + i)) !=
null) null)
{ {
Exception exception = null; Exception exception = null;
@ -97,7 +97,6 @@ public final class Security extends Object
try try
{ {
providers.addElement(Class.forName(name).newInstance()); providers.addElement(Class.forName(name).newInstance());
i++;
} }
catch (ClassNotFoundException x) catch (ClassNotFoundException x)
{ {
@ -114,6 +113,7 @@ public final class Security extends Object
if (exception != null) if (exception != null)
System.err.println ("Error loading security provider " + name System.err.println ("Error loading security provider " + name
+ ": " + exception); + ": " + exception);
i++;
} }
} }
catch (FileNotFoundException ignored) catch (FileNotFoundException ignored)