1 /*
   2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  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                 "Hash_DRBG,SHA-512,192,pr_and_reseed"}) {
  38 
  39             System.out.println("Testing " + alg);
  40             SecureRandom s1;
  41 
  42             // A SecureRandom can be s11ned and des11ned at any time.
  43 
  44             // Brand new.
  45             s1 = getInstance(alg);
  46             revive(s1).nextInt();
  47             if (alg.contains("DRBG")) {
  48                 revive(s1).reseed();
  49             }
  50 
  51             // Used
  52             s1 = getInstance(alg);
  53             s1.nextInt();    // state set
  54             revive(s1).nextInt();
  55             if (alg.contains("DRBG")) {
  56                 revive(s1).reseed();
  57             }
  58 
  59             // Automatically reseeded
  60             s1 = getInstance(alg);
  61             if (alg.contains("DRBG")) {
  62                 s1.reseed();
  63             }
  64             revive(s1).nextInt();
  65             if (alg.contains("DRBG")) {
  66                 revive(s1).reseed();
  67             }
  68 
  69             // Manually seeded
  70             s1 = getInstance(alg);
  71             s1.setSeed(1L);
  72             revive(s1).nextInt();
  73             if (alg.contains("DRBG")) {
  74                 revive(s1).reseed();
  75             }
  76         }
  77     }
  78 
  79     private static SecureRandom getInstance(String alg) throws Exception {
  80         if (alg.equals("SHA1PRNG") || alg.equals("DRBG")) {
  81             return SecureRandom.getInstance(alg);
  82         } else {
  83             String old = Security.getProperty("securerandom.drbg.config");
  84             try {
  85                 Security.setProperty("securerandom.drbg.config", alg);
  86                 return SecureRandom.getInstance("DRBG");
  87             } finally {
  88                 Security.setProperty("securerandom.drbg.config", old);
  89             }
  90         }
  91     }
  92 
  93     private static SecureRandom revive(SecureRandom oldOne) throws Exception {
  94         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  95         new ObjectOutputStream(bout).writeObject(oldOne);
  96         SecureRandom newOne = (SecureRandom) new ObjectInputStream(
  97                 new ByteArrayInputStream(bout.toByteArray())).readObject();
  98         if (!oldOne.toString().equals(newOne.toString())) {
  99             throw new Exception(newOne + " is not " + oldOne);
 100         }
 101         return newOne;
 102     }
 103 }