This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: SecureRandom



The Random constructor calls setSeed() at a time when SecureRandom has
not yet initilialized secureRandomSpi, resulting in a
NullPointerException.   This tweak fixes..

Ok to commit?


2001-07-09  Anthony Green  <green@redhat.com>

	* java/security/SecureRandom.java: Store the seed array until
	we're ready to use it.


Index: java/security/SecureRandom.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/security/SecureRandom.java,v
retrieving revision 1.2
diff -u -p -r1.2 SecureRandom.java
--- SecureRandom.java	2001/04/25 15:45:12	1.2
+++ SecureRandom.java	2001/07/11 02:32:56
@@ -1,5 +1,5 @@
 /* SecureRandom.java --- Secure Random class implmentation
-   Copyright (C) 1999 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -36,7 +36,8 @@ import java.util.Enumeration;
 
    @author Mark Benvenuto <ivymccough@worldnet.att.net>
  */
-public class SecureRandom extends Random
+public class SecureRandom
+ extends Random
 {
   //Serialized Field
   long counter = 0;		//Serialized
@@ -87,6 +88,10 @@ public class SecureRandom extends Random
 
     try
       {
+	Class c = Class.forName (classname);
+
+	SecureRandomSpi srspi = (SecureRandomSpi) c.newInstance ();
+
 	this.secureRandomSpi =
 	  (SecureRandomSpi) Class.forName(classname).newInstance();
 
@@ -105,6 +110,12 @@ public class SecureRandom extends Random
       {
 	//throw new NoSuchAlgorithmException("Illegal Access");
       }
+
+    if (state != null)
+      {
+	setSeed (state);
+	state = null;
+      }
   }
 
   /**
@@ -172,6 +183,10 @@ public class SecureRandom extends Random
 
     try
       {
+	Class c = Class.forName (classname);
+
+	SecureRandomSpi srspi = (SecureRandomSpi) c.newInstance ();
+
 	return new SecureRandom((SecureRandomSpi) Class.forName(classname).
 				newInstance(), p[i]);
       }
@@ -187,7 +202,6 @@ public class SecureRandom extends Random
       {
 	throw new NoSuchAlgorithmException("Illegal Access");
       }
-
   }
 
   /**
@@ -267,12 +281,18 @@ public class SecureRandom extends Random
    */
   public void setSeed(long seed)
   {
-    byte tmp[] = { (byte) (0xff & (seed >> 56)), (byte) (0xff & (seed >> 48)),
-      (byte) (0xff & (seed >> 40)), (byte) (0xff & (seed >> 32)),
-      (byte) (0xff & (seed >> 24)), (byte) (0xff & (seed >> 16)),
-      (byte) (0xff & (seed >> 8)), (byte) (0xff & seed)
-    };
-    secureRandomSpi.engineSetSeed(tmp);
+    state = new byte[8];
+    state[0] = (byte) (0xff & (seed >> 56));
+    state[1] = (byte) (0xff & (seed >> 48));
+    state[2] = (byte) (0xff & (seed >> 40));
+    state[3] = (byte) (0xff & (seed >> 32));
+    state[4] = (byte) (0xff & (seed >> 24));
+    state[5] = (byte) (0xff & (seed >> 16));
+    state[6] = (byte) (0xff & (seed >> 8));
+    state[7] = (byte) (0xff & seed);
+
+    if (secureRandomSpi != null)
+      secureRandomSpi.engineSetSeed(state);
   }
 
   /**


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]