Path: blob/master/test/jdk/sun/security/pkcs11/SecureRandom/TestDeserialization.java
41152 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 683784726* @summary Ensure a deserialized PKCS#11 SecureRandom is functional.27* @library /test/lib ..28* @modules jdk.crypto.cryptoki29*/3031import java.io.ByteArrayInputStream;32import java.io.ByteArrayOutputStream;33import java.io.ObjectInputStream;34import java.io.ObjectOutputStream;35import java.security.NoSuchAlgorithmException;36import java.security.Provider;37import java.security.SecureRandom;38import java.security.Security;3940public class TestDeserialization extends PKCS11Test {4142public void main(Provider p) throws Exception {43// Skip this test for providers not found by java.security.Security44if (Security.getProvider(p.getName()) != p) {45System.out.println("Skip test for provider " + p.getName());46return;47}48SecureRandom r;49try {50r = SecureRandom.getInstance("PKCS11", p);51System.out.println("SecureRandom instance " + r);52} catch (NoSuchAlgorithmException e) {53System.out.println("Provider " + p +54" does not support SecureRandom, skipping");55e.printStackTrace();56return;57}58r.setSeed(System.currentTimeMillis());59byte[] buf = new byte[16];60byte[] ser = toByteArray(r);61System.out.println("Serialized Len = " + ser.length);62SecureRandom r2 = fromByteArray(ser);63System.out.println("Deserialized into " + r2);64r2.nextBytes(buf);65System.out.println("Done");66}6768public static void main(String[] args) throws Exception {69main(new TestDeserialization());70}7172private byte[] toByteArray(SecureRandom r) throws Exception {73ByteArrayOutputStream out = new ByteArrayOutputStream(1024);74ObjectOutputStream outStream = null;75try {76outStream = new ObjectOutputStream(out);77outStream.writeObject(r);78return out.toByteArray();79} finally {80if (outStream != null) {81outStream.close();82}83}84}8586private SecureRandom fromByteArray(byte[] buf) throws Exception {87SecureRandom r = null;88ByteArrayInputStream is = new ByteArrayInputStream(buf);89ObjectInputStream ois = null;90try {91ois = new ObjectInputStream(is);92r = (SecureRandom) ois.readObject();93} finally {94if (ois != null) {95ois.close();96}97}98return r;99}100}101102103