test/jdk/java/security/SecureRandom/Serialize.java
author shade
Tue, 15 Oct 2019 19:38:59 +0200
changeset 58604 791217cdc433
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232205: Shenandoah: missing "Update References" -> "Update Roots" tracing Reviewed-by: rkennke, zgu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4102896
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Make sure that a SecureRandom object can be serialized
38437
0401899aa994 8157344: Multiple test timeouts after push for JDK-8141039
asmotrak
parents: 37796
diff changeset
    27
 * @run main/othervm Serialize
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Serialize {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    35
    public static void main(String args[]) throws Exception {
38437
0401899aa994 8157344: Multiple test timeouts after push for JDK-8141039
asmotrak
parents: 37796
diff changeset
    36
        System.setProperty("java.security.egd", "file:/dev/urandom");
0401899aa994 8157344: Multiple test timeouts after push for JDK-8141039
asmotrak
parents: 37796
diff changeset
    37
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    38
        for (String alg: new String[]{
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    39
                "SHA1PRNG", "DRBG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG",
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    40
                "Hash_DRBG,SHA-512,192,pr_and_reseed"}) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    41
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    42
            System.out.println("Testing " + alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    43
            SecureRandom s1;
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    44
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    45
            // A SecureRandom can be s11ned and des11ned at any time.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    47
            // Brand new.
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    48
            s1 = getInstance(alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    49
            revive(s1).nextInt();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    50
            if (alg.contains("DRBG")) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    51
                revive(s1).reseed();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    52
            }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    53
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    54
            // Used
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    55
            s1 = getInstance(alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    56
            s1.nextInt();    // state set
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    57
            revive(s1).nextInt();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    58
            if (alg.contains("DRBG")) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    59
                revive(s1).reseed();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    60
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    62
            // Automatically reseeded
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    63
            s1 = getInstance(alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    64
            if (alg.contains("DRBG")) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    65
                s1.reseed();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    66
            }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    67
            revive(s1).nextInt();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    68
            if (alg.contains("DRBG")) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    69
                revive(s1).reseed();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    70
            }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    71
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    72
            // Manually seeded
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    73
            s1 = getInstance(alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    74
            s1.setSeed(1L);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    75
            revive(s1).nextInt();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    76
            if (alg.contains("DRBG")) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    77
                revive(s1).reseed();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    78
            }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    79
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    80
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
37796
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    82
    private static SecureRandom getInstance(String alg) throws Exception {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    83
        if (alg.equals("SHA1PRNG") || alg.equals("DRBG")) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    84
            return SecureRandom.getInstance(alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    85
        } else {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    86
            String old = Security.getProperty("securerandom.drbg.config");
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    87
            try {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    88
                Security.setProperty("securerandom.drbg.config", alg);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    89
                return SecureRandom.getInstance("DRBG");
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    90
            } finally {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    91
                Security.setProperty("securerandom.drbg.config", old);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    92
            }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    93
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    94
    }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    95
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    96
    private static SecureRandom revive(SecureRandom oldOne) throws Exception {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    97
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    98
        new ObjectOutputStream(bout).writeObject(oldOne);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
    99
        SecureRandom newOne = (SecureRandom) new ObjectInputStream(
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
   100
                new ByteArrayInputStream(bout.toByteArray())).readObject();
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
   101
        if (!oldOne.toString().equals(newOne.toString())) {
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
   102
            throw new Exception(newOne + " is not " + oldOne);
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
   103
        }
256c45c4af5d 8051408: NIST SP 800-90A SecureRandom implementations
weijun
parents: 5506
diff changeset
   104
        return newOne;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}