Path: blob/master/test/jdk/java/security/KeyPairGenerator/Failover.java
41149 views
/*1* Copyright (c) 2003, 2016, 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 4894125 7054918 813018126* @library ../testlibrary27* @summary test that failover for KeyPairGenerator works28* @author Andreas Sterbenz29*/3031import java.util.*;3233import java.security.*;34import java.security.interfaces.*;35import java.security.spec.*;3637public class Failover {3839public static void main(String[] args) throws Exception {40ProvidersSnapshot snapshot = ProvidersSnapshot.create();41try {42main0(args);43} finally {44snapshot.restore();45}46}4748public static void main0(String[] args) throws Exception {49Security.insertProviderAt(new ProviderFail(), 1);50Security.addProvider(new ProviderPass());51System.out.println(Arrays.asList(Security.getProviders()));5253KeyPairGenerator kpg;54kpg = KeyPairGenerator.getInstance("FOO");55kpg.generateKeyPair();56kpg.generateKeyPair();5758kpg = KeyPairGenerator.getInstance("FOO");59kpg.initialize(1024);60kpg.initialize(1024);61kpg.initialize(null, null);62kpg.generateKeyPair();6364kpg = KeyPairGenerator.getInstance("FOO");65kpg.initialize(null, null);66kpg.generateKeyPair();6768kpg = KeyPairGenerator.getInstance("FOO");69kpg.initialize(512);70kpg.generateKeyPair();71kpg.generateKeyPair();7273// the SUN DSA KeyPairGenerator implementation extends74// KeyPairGenerator (in order to implement75// java.security.interfaces.DSAKeyPairGenerator)76// failover cannot work77kpg = KeyPairGenerator.getInstance("DSA");78try {79kpg.initialize(1024);80throw new Exception("no exception");81} catch (InvalidParameterException e) {82System.out.println(e);83}8485KeyPair kp;86kpg = KeyPairGenerator.getInstance("RSA");87kpg.initialize(512);88kp = kpg.generateKeyPair();89System.out.println(kp.getPublic());9091kpg = KeyPairGenerator.getInstance("RSA");92kpg.initialize(768);93kp = kpg.generateKeyPair();94System.out.println(kp.getPublic());9596kpg = KeyPairGenerator.getInstance("RSA");97kp = kpg.generateKeyPair();98System.out.println(kp.getPublic());99100kpg = KeyPairGenerator.getInstance("RSA");101try {102kpg.initialize(128);103throw new Exception("no exception");104} catch (InvalidParameterException e) {105System.out.println(e);106}107108}109110private static class ProviderPass extends Provider {111ProviderPass() {112super("Pass", "1.0", "Pass");113put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorPass");114}115}116117private static class ProviderFail extends Provider {118ProviderFail() {119super("Fail", "1.0", "Fail");120put("KeyPairGenerator.FOO" , "Failover$KeyPairGeneratorFail");121put("KeyPairGenerator.DSA" , "Failover$KeyPairGeneratorFail");122put("KeyPairGenerator.RSA" , "Failover$KeyPairGeneratorFail");123}124}125126public static class KeyPairGeneratorPass extends KeyPairGeneratorSpi {127128public void initialize(int keysize, SecureRandom random) {129System.out.println("KeyPairGeneratorPass.initialize(" + keysize + ", " + random + ")");130}131132public void initialize(AlgorithmParameterSpec params,133SecureRandom random) throws InvalidAlgorithmParameterException {134System.out.println("KeyPairGeneratorPass.initialize()");135}136137public KeyPair generateKeyPair() {138System.out.println("KeyPairGeneratorPass.generateKeyPair()");139return null;140}141142}143144public static class KeyPairGeneratorFail extends KeyPairGeneratorSpi {145146public void initialize(int keysize, SecureRandom random) {147if (keysize != 512) {148System.out.println("KeyPairGeneratorFail.initialize()");149throw new InvalidParameterException();150}151System.out.println("KeyPairGeneratorFail.initialize() PASS");152}153154public void initialize(AlgorithmParameterSpec params,155SecureRandom random) throws InvalidAlgorithmParameterException {156System.out.println("KeyPairGeneratorFail.initialize()");157throw new InvalidParameterException();158}159160public KeyPair generateKeyPair() {161System.out.println("KeyPairGeneratorFail.generateKeyPair()");162throw new InvalidParameterException();163}164165}166167}168169170