jdk/test/java/security/KeyPairGenerator/FinalizeHalf.java
author igerasim
Mon, 15 Aug 2016 23:45:32 +0300
changeset 40389 c6df8bba0b71
permissions -rw-r--r--
8163896: Finalizing one key of a KeyPair invalidates the other key Reviewed-by: coffeys, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     1
/*
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     4
 *
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     8
 *
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    13
 * accompanied this code).
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    14
 *
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    18
 *
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    21
 * questions.
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    22
 */
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    23
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    24
/**
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    25
 * @test
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    26
 * @bug 8163896
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    27
 * @summary Finalizing one key of a KeyPair invalidates the other key
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    28
 */
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    29
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    30
import java.security.Key;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    31
import java.security.KeyPair;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    32
import java.security.KeyPairGenerator;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    33
import java.security.NoSuchAlgorithmException;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    34
import java.security.Provider;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    35
import java.security.ProviderException;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    36
import java.security.Security;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    37
import java.util.function.Consumer;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    38
import java.util.ArrayList;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    39
import java.util.List;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    40
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    41
public class FinalizeHalf {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    42
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    43
    static int failures = 0;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    44
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    45
    public static void main(String[] args) throws Throwable {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    46
        List<Consumer<Key>> methods = new ArrayList<>();
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    47
        methods.add((Key k) -> k.getEncoded());
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    48
        methods.add((Key k) -> k.toString());
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    49
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    50
        for (String algo : new String[] {"DiffieHellman", "DSA", "RSA"}) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    51
            for (Provider provider : Security.getProviders()) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    52
                for (boolean priv : new boolean[] {true, false}) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    53
                    for (Consumer<Key> method : methods) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    54
                        test(algo, provider, priv, method);
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    55
                    }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    56
                }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    57
            }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    58
        }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    59
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    60
        if (failures > 0) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    61
            throw new RuntimeException(failures + " test(s) failed.");
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    62
        }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    63
    }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    64
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    65
    static void test(String algo, Provider provider, boolean priv,
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    66
            Consumer<Key> method) throws Exception {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    67
        KeyPairGenerator generator;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    68
        try {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    69
            generator = KeyPairGenerator.getInstance(algo, provider);
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    70
        } catch (NoSuchAlgorithmException nsae) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    71
            return;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    72
        }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    73
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    74
        System.out.println("Checking " + provider.getName() + ", " + algo);
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    75
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    76
        KeyPair pair = generator.generateKeyPair();
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    77
        Key key = priv ? pair.getPrivate() : pair.getPublic();
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    78
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    79
        pair = null;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    80
        for (int i = 0; i < 32; ++i) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    81
            System.gc();
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    82
        }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    83
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    84
        try {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    85
            method.accept(key);
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    86
        } catch (ProviderException pe) {
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    87
            failures++;
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    88
        }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    89
    }
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents:
diff changeset
    90
}