1/*
2 * Copyright (c) 2010, 2017, 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/*
25 * @test
26 * @bug 6837847
27 * @summary Ensure a deserialized PKCS#11 SecureRandom is functional.
28 * @library ..
29 * @modules jdk.crypto.cryptoki
30 */
31
32import java.io.ByteArrayInputStream;
33import java.io.ByteArrayOutputStream;
34import java.io.ObjectInputStream;
35import java.io.ObjectOutputStream;
36import java.security.NoSuchAlgorithmException;
37import java.security.Provider;
38import java.security.SecureRandom;
39import java.security.Security;
40
41public class TestDeserialization extends PKCS11Test {
42
43    public void main(Provider p) throws Exception {
44        // Skip this test for providers not found by java.security.Security
45        if (Security.getProvider(p.getName()) != p) {
46            System.out.println("Skip test for provider " + p.getName());
47            return;
48        }
49        SecureRandom r;
50        try {
51            r = SecureRandom.getInstance("PKCS11", p);
52            System.out.println("SecureRandom instance " + r);
53        } catch (NoSuchAlgorithmException e) {
54            System.out.println("Provider " + p +
55                               " does not support SecureRandom, skipping");
56            e.printStackTrace();
57            return;
58        }
59        r.setSeed(System.currentTimeMillis());
60        byte[] buf = new byte[16];
61        byte[] ser = toByteArray(r);
62        System.out.println("Serialized Len = " + ser.length);
63        SecureRandom r2 = fromByteArray(ser);
64        System.out.println("Deserialized into " + r2);
65        r2.nextBytes(buf);
66        System.out.println("Done");
67    }
68
69    public static void main(String[] args) throws Exception {
70        main(new TestDeserialization());
71    }
72
73    private byte[] toByteArray(SecureRandom r) throws Exception {
74        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
75        ObjectOutputStream outStream = null;
76        try {
77            outStream = new ObjectOutputStream(out);
78            outStream.writeObject(r);
79            return out.toByteArray();
80        } finally {
81            if (outStream != null) {
82                outStream.close();
83            }
84        }
85    }
86
87    private SecureRandom fromByteArray(byte[] buf) throws Exception {
88        SecureRandom r = null;
89        ByteArrayInputStream is = new ByteArrayInputStream(buf);
90        ObjectInputStream ois = null;
91        try {
92            ois = new ObjectInputStream(is);
93            r = (SecureRandom) ois.readObject();
94        } finally {
95            if (ois != null) {
96                ois.close();
97            }
98        }
99        return r;
100    }
101}
102