14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /* @test
25 * @bug 4102896
26 * @summary Make sure that a SecureRandom object can be serialized
27 */
28
29 import java.security.*;
30 import java.io.*;
31
32 public class Serialize {
33
34 public static void main(String args[]) throws IOException {
35
36 FileOutputStream fos = new FileOutputStream("t.tmp");
37 ObjectOutputStream oos = new ObjectOutputStream(fos);
38
39 SecureRandom secRandom = new SecureRandom();
40
41 // serialize and write out
42 oos.writeObject(secRandom);
43 oos.flush();
44 oos.close();
45 }
46 }
|
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /* @test
25 * @bug 4102896
26 * @summary Make sure that a SecureRandom object can be serialized
27 */
28
29 import java.security.*;
30 import java.io.*;
31
32 public class Serialize {
33
34 public static void main(String args[]) throws Exception {
35 for (String alg: new String[]{
36 "SHA1PRNG", "DRBG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
37 System.out.println("Testing " + alg);
38
39 // Even unseeded object can be serialized and deserialized
40 SecureRandom s1 = getInstance(alg);
41 revive(s1).nextInt();
42 if (alg.contains("DRBG")) {
43 revive(s1).reseed();
44 }
45
46 // After seeded, deserialized object should emit same random data
47 s1 = getInstance(alg);
48 s1.nextInt(); // state set
49 SecureRandom s2 = revive(s1);
50 int n1 = s1.nextInt();
51 int n2 = s2.nextInt();
52 if (n1 != n2) {
53 throw new Exception();
54 }
55
56 // Or seeded with user-provided data
57 s1.setSeed(42);
58 s2.setSeed(42);
59 n1 = s1.nextInt();
60 n2 = s2.nextInt();
61 if (n1 != n2) {
62 throw new Exception();
63 }
64
65 // But not after automatic reseed
66 if (alg.contains("DRBG")) {
67 s1.reseed();
68 s2.reseed();
69 n1 = s1.nextInt();
70 n2 = s2.nextInt();
71 if (n1 == n2) {
72 throw new Exception();
73 }
74 }
75 }
76 }
77
78 private static SecureRandom getInstance(String alg) throws Exception {
79 if (alg.equals("SHA1PRNG") || alg.equals("DRBG")) {
80 return SecureRandom.getInstance(alg);
81 } else {
82 String old = Security.getProperty("drbg");
83 try {
84 Security.setProperty("drbg", alg);
85 return SecureRandom.getInstance("DRBG");
86 } finally {
87 Security.setProperty("drbg", old);
88 }
89 }
90 }
91
92 private static SecureRandom revive(SecureRandom sr) throws Exception {
93 ByteArrayOutputStream bout = new ByteArrayOutputStream();
94 new ObjectOutputStream(bout).writeObject(sr);
95 return (SecureRandom) new ObjectInputStream(
96 new ByteArrayInputStream(bout.toByteArray())).readObject();
97 }
98 }
|