1/*
2 * Copyright (c) 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/**
25 * @test
26 * @bug 8163896
27 * @summary Finalizing one key of a KeyPair invalidates the other key
28 */
29
30import java.security.Key;
31import java.security.KeyPair;
32import java.security.KeyPairGenerator;
33import java.security.NoSuchAlgorithmException;
34import java.security.Provider;
35import java.security.ProviderException;
36import java.security.Security;
37import java.util.function.Consumer;
38import java.util.ArrayList;
39import java.util.List;
40
41public class FinalizeHalf {
42
43    static int failures = 0;
44
45    public static void main(String[] args) throws Throwable {
46        List<Consumer<Key>> methods = new ArrayList<>();
47        methods.add((Key k) -> k.getEncoded());
48        methods.add((Key k) -> k.toString());
49
50        for (String algo : new String[] {"DiffieHellman", "DSA", "RSA"}) {
51            for (Provider provider : Security.getProviders()) {
52                for (boolean priv : new boolean[] {true, false}) {
53                    for (Consumer<Key> method : methods) {
54                        test(algo, provider, priv, method);
55                    }
56                }
57            }
58        }
59
60        if (failures > 0) {
61            throw new RuntimeException(failures + " test(s) failed.");
62        }
63    }
64
65    static void test(String algo, Provider provider, boolean priv,
66            Consumer<Key> method) throws Exception {
67        KeyPairGenerator generator;
68        try {
69            generator = KeyPairGenerator.getInstance(algo, provider);
70        } catch (NoSuchAlgorithmException nsae) {
71            return;
72        }
73
74        System.out.println("Checking " + provider.getName() + ", " + algo);
75
76        KeyPair pair = generator.generateKeyPair();
77        Key key = priv ? pair.getPrivate() : pair.getPublic();
78
79        pair = null;
80        for (int i = 0; i < 32; ++i) {
81            System.gc();
82        }
83
84        try {
85            method.accept(key);
86        } catch (ProviderException pe) {
87            failures++;
88        }
89    }
90}
91