2236N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2236N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2236N/A *
2236N/A * This code is free software; you can redistribute it and/or modify it
2236N/A * under the terms of the GNU General Public License version 2 only, as
2236N/A * published by the Free Software Foundation.
2236N/A *
2236N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2236N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2236N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2236N/A * version 2 for more details (a copy is included in the LICENSE file that
2236N/A * accompanied this code).
2236N/A *
2236N/A * You should have received a copy of the GNU General Public License version
2236N/A * 2 along with this work; if not, write to the Free Software Foundation,
2236N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2236N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2236N/A */
2236N/A
2236N/A/**
2236N/A * @test
2236N/A * @bug 6837847
2236N/A * @summary Ensure a deserialized PKCS#11 SecureRandom is functional.
2236N/A * @library ..
2236N/A */
2236N/A
2236N/Aimport java.security.*;
2236N/Aimport java.io.*;
2236N/A
2236N/Apublic class TestDeserialization extends PKCS11Test {
2236N/A
2236N/A public void main(Provider p) throws Exception {
2236N/A // Skip this test for providers not found by java.security.Security
2236N/A if (Security.getProvider(p.getName()) != p) {
2236N/A System.out.println("Skip test for provider " + p.getName());
2236N/A return;
2236N/A }
2236N/A SecureRandom r;
2236N/A try {
2236N/A r = SecureRandom.getInstance("PKCS11", p);
2236N/A System.out.println("SecureRandom instance " + r);
2236N/A } catch (NoSuchAlgorithmException e) {
2236N/A System.out.println("Provider " + p +
2236N/A " does not support SecureRandom, skipping");
2236N/A e.printStackTrace();
2236N/A return;
2236N/A }
2236N/A r.setSeed(System.currentTimeMillis());
2236N/A byte[] buf = new byte[16];
2236N/A byte[] ser = toByteArray(r);
2236N/A System.out.println("Serialized Len = " + ser.length);
2236N/A SecureRandom r2 = fromByteArray(ser);
2236N/A System.out.println("Deserialized into " + r2);
2236N/A r2.nextBytes(buf);
2236N/A System.out.println("Done");
2236N/A }
2236N/A
2236N/A public static void main(String[] args) throws Exception {
2236N/A main(new TestDeserialization());
2236N/A }
2236N/A
2236N/A private byte[] toByteArray(SecureRandom r) throws Exception {
2236N/A ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
2236N/A ObjectOutputStream outStream = null;
2236N/A try {
2236N/A outStream = new ObjectOutputStream(out);
2236N/A outStream.writeObject(r);
2236N/A return out.toByteArray();
2236N/A } finally {
2236N/A if (outStream != null) {
2236N/A outStream.close();
2236N/A }
2236N/A }
2236N/A }
2236N/A
2236N/A private SecureRandom fromByteArray(byte[] buf) throws Exception {
2236N/A SecureRandom r = null;
2236N/A ByteArrayInputStream is = new ByteArrayInputStream(buf);
2236N/A ObjectInputStream ois = null;
2236N/A try {
2236N/A ois = new ObjectInputStream(is);
2236N/A r = (SecureRandom) ois.readObject();
2236N/A } finally {
2236N/A if (ois != null) {
2236N/A ois.close();
2236N/A }
2236N/A }
2236N/A return r;
2236N/A }
2236N/A}